Reputation: 313
I have the following model in my django project (the objects are basically a list of dictionaries):
def bill_sum(request):
titles = Summary.objects.values('title')
summary = Summary.objects.values('summary')
summary_text = Summary.objects.values('summary_text')
action_date = Summary.objects.values('action_date')
action_desc = Summary.objects.values('action_desc')
context = {'title':titles,
'summary':summary,
'summary_text':summary_text,
'action_date': action_date,
'action_desc':action_desc,
}
return render(request,'billsummary.html',context)
In my template I can generate a list of titles by the following html code:
<!doctype html>
<html>
<body>
{% for t in title %}
<h1>{{t.title}}</h1>
{% endfor %}
</body>
</html>
However, when I try to add additional information (such as a loop for all the summary objects, nothing renders except the titles. What am I missing? How do I render a list of dictionaries (i.e. a Json data file) from my views into a template?
What I want to do is something like the following in the template:
{% for c in context %}
<h1>c.title</h1>>
<h2>c.summary</h2>
<p>c.action_date</p>
{% endfor %}
Upvotes: 0
Views: 1557
Reputation: 1868
You could do:
summaries = Summary.objects.values('title', 'summary', 'summary_text', 'action_date', 'action_desc')
return render(request,'billsummary.html',context={'summaries': summaries})
Then in the template:
{% for t in summaries %}
<h1>{{t.title}}</h1>
<p>{{t.summary}}</p>
{% endfor %}
It will hit the database only only one time and will be perfect for your case
Upvotes: 1