Reputation: 313
I have a json file 'data' which is a list of dictionaries. One of the keys in every dictionary is 'title' whose value is a a list of titles. How do I display just the first 'title' in this list for every dictionary? The json is as follows:
[{'title':['title1','title2,..],'other data':'xyz',...,}...{'title n':['title n1','title n2,..],'other data n1':'xyz n2',...,}
This is my views.py:
def bill_status(request):
data = Status.objects.all()
context = {'data':data}
return render(request,'billstatus.html',context)
In my template I am rendering this as:
{% for datum in data %}
<h3>{{datum.title}}</h3>
{% endfor %}
However, the output in html is the entire list for every dictionary:
['title1','title2,..]
How do I just output 'title1' rather than the entire list?
Upvotes: 0
Views: 576
Reputation: 182
{{ datum.title.0 }} would do the trick for you.
Example:-
dat = [{'title':['t1','t2']}, {'title':['t3','t4']}];
In template
{% for dict in dat %}
{{dict.title.0}}
{% endfor %}
Output
t1 t3
Upvotes: 1
Reputation: 12086
That would be <h3>{{ datum.title.0 }}</h3>
to get the first item (0
) of the list.
You may read more in the docs about the Django Template Language variables.
Upvotes: 1