7

The thing I really hate when learning a new language / framework is how ignorant I feel when I get stuck on a seemingly easy to solve issue.

I have a django for loop inside a html page but for some reason it is not working. I have missed something and cannot fix the issue on my own, so I turn to StackOverflow to help me.

This is my model I am running my query on models.py:

class RIAchievement(models.Model):
  riAchievementID = models.AutoField(primary_key=True, db_column="RIAchievementID")
  userLanguageVersionID = models.ForeignKey(UserLanguageVersion, db_column="UserLanguageVersionID")
  typeAchievementID = models.ForeignKey(TypeAchievement, db_column="TypeAchievementID")
  riAchievementTypeUserDescription = models.CharField(max_length=255, blank=True, null=True, db_column="RIAchievementTypeUserDescription")
  riAchievementDescription = models.TextField(max_length=2000, db_column="RIAchievementDescription")
  auth_user_id = models.ForeignKey(auth_user, db_column="auth_user_id")
  class Meta:
    db_table="RIAchievement"

This is where my models.py file is located in my project: GlobalXpy\app_data\models.py

This is the code within my views.py file:

from django.shortcuts import render_to_response
from GlobalXpy.app_data.models import RIAchievement

def index(request):
  ri_achievement = RIAchievement.objects.all()
  get_template = loader.get_template('ri_achievement.html')
  return render_to_response(get_template)

This is the for loop that is inside my template file (ri_achievement.html):

{% for e in ri_achievement %}
  <td> Preview  Edit  Duplicate  Delete </td>
  <td> FlagPath </td>
  <td> AchievementType / RIAchievementTypeUserDescription </td>
  <td> {{ e.riAchievementDescription }} </td>
{% endfor %}

Any assistance would be appreciated.

user1261774
  • 3,525
  • 10
  • 55
  • 103
  • Please clarify in what way it is not working. Is it that you navigate to the page, but then you see different output from what you expected? – Andrew Gorcester Apr 26 '12 at 01:45
  • Where the for loop should display the output, no data is displayed. The for loop is not being undertaken. – user1261774 Apr 26 '12 at 02:18
  • First print the `ri_achievement` in template before for loop. Check it contains data or not? – Ahsan Apr 26 '12 at 06:13
  • to print the ri_achievement, do I simply write: {{ ri_achievement }} if so, this prints / displays nothing. – user1261774 Apr 26 '12 at 07:01

2 Answers2

15
from django.shortcuts import render
from GlobalXpy.app_data.models import RIAchievement

def index(request):
  ri_achievement = RIAchievement.objects.all()
  return render(request, 'ri_achievement.html',{'ri_achievement': ri_achievement})

In your template:

{% if ri_achievement %}
   There are {{ ri_achievement|length }} records:
   {% for e in ri_achievement %}
      <td> Preview  Edit  Duplicate  Delete </td>
      <td> FlagPath </td>
      <td> AchievementType / RIAchievementTypeUserDescription </td>
      <td> {{ e.riAchievementDescription }} </td>
   {% endfor %}
{% else %}
   There are no records in the system
{% endif %}
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284
  • burhan, thanks for the advice. I get the message that there are no records in the system (even though there are 10 records that should be displayed). What should I check to confirm that the code is calling the module / database correctly? – user1261774 Apr 26 '12 at 09:21
  • If you are getting "There are no records in the system", then the code is working correctly. Start a django shell `manage.py shell`, then `from GlobalXpy.app_data.models import RIAchievement` followed by `RIAchievement.objects.all().count()` - what does that print? – Burhan Khalid Apr 26 '12 at 10:49
  • That prints 10 - the number of records in the database. – user1261774 Apr 26 '12 at 12:11
  • Burhan, thanks for the solution. This did not immediately work for me. So I deleted my project and database and installed a fresh installation of my django project & database / models file, which then worked. I did not find the issue that caused the problem. – user1261774 May 07 '12 at 22:07
2

You forgot to pass the variable.

return render_to_response(get_template, {'ri_achievement': ri_achievement})
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358