9

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.

4
  • 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? Commented Apr 26, 2012 at 1:45
  • Where the for loop should display the output, no data is displayed. The for loop is not being undertaken. Commented Apr 26, 2012 at 2:18
  • First print the ri_achievement in template before for loop. Check it contains data or not? Commented Apr 26, 2012 at 6:13
  • to print the ri_achievement, do I simply write: <td> {{ ri_achievement }} </td> if so, this prints / displays nothing. Commented Apr 26, 2012 at 7:01

2 Answers 2

16
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 %} 
Sign up to request clarification or add additional context in comments.

4 Comments

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?
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?
That prints 10 - the number of records in the database.
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.
2

You forgot to pass the variable.

return render_to_response(get_template, {'ri_achievement': ri_achievement}) 

1 Comment

I tried your suggestion, but the for loop does not execute (no data is displayed).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.