Reputation: 1169
I'm trying to access key value pairs from a dictionary in a Django template, but am facing some issues.
The dictionary looks something like this:
{datetime.datetime(2014, 10, 31, 0, 0, tzinfo=<UTC>): [<Item: do something1>],
datetime.datetime(2014, 10, 24, 0, 0, tzinfo=<UTC>): [<Item: dosomething2>,<Item: dosomething3>]}
Django template:
{% if item_list %}
<ul>
{{item_list}}
{% for key, value in item_list %}
<h3>{{key}}</h3>
{% for item in value %}
<input type="checkbox" value="{{item.title}}">{{item.title}}<br>
{% endfor %}
{% endfor %}
</ul>
{% else %}
<p>No items are available.</p>
{% endif %}
Views.py
def index(request):
try:
item_list = sort_items_by_date()
except Item.DoesNotExist:
raise Http404
return render(request, 'index.html', {'item_list':item_list, 'new_item_form':ItemForm()})
When the template is rendered, none of the items from the dictionary show up. I looked up other similar questions here and tried their solutions (e.g. using item_list.date) but it didn't work. I'd really appreciate it if someone could point out what I'm doing wrong here
Upvotes: 0
Views: 832
Reputation: 799490
{% for key, value in item_list %}
Iterating over a dictionary yields keys.
{% for key, value in item_list.iteritems %}
Upvotes: 1