Django Template Can't Loop Defaultdict

Django template can't loop defaultdict

try:

dict(new_data)

and in Python 2 it is better to use iteritems instead of items :)

My defaultdict(list) won't show up on template but does in my view

This happens because of the way the Django template language does variable lookups. When you try to loop through the dictionaries items,

{% for key, value in confirmlist.items %}

Django first does a dictionary lookup for confirmlist['items']. As this is a defaultdict, an empty list is returned.

It's a cruel gotcha, that I've been stung by as well!

To work around this problem, convert your defaultdict into a dictionary before adding it to the template context.

context['confirmlist'] = dict(confirm_list)

Or, as explained by sebastien trottier in his his answer to a similar question, set
default_factory to None before adding to the template context.

confirm_list.default_factory = None
context['confirmlist'] = confirm_list

.items not working on defaultdict in Django template

This is a known issue in Django: you cannot iterate over a defaultdict in a template. The docs suggest that the best way to handle this is to convert your defaultdict to a dict before passing it to the template:

context['data'] = dict(assertion_dict)

The reason it doesn't work, by the way, is that when you call {{ data.items }} in your template, Django will first attempt to find data['items'], and then data.items. The defaultdict will return a default value for the former, so Django will not attempt the latter, and you end up trying to loop over the default value instead of the dict.

Weird runtime error when looping over DefaultDict in django template

Using defaultdicts causes weird behaviour in Django templates, because of the way template variable lookups work. See the Behind the scenes box of the Django docs.

The Django docs suggest converting the defaultdict to a regular dict before passing to the template.

count_by_media_type = defaultdict(int)
for user_media in user_media_data:
count_by_media_type[user_media['media_type']] += 1

count_by_media_type = dict(count_by_media_type)

Or, as this answer suggests, you can disable the defaulting feature after you have finished inserting values.

count_by_media_type.default_factory = None

Looping problem with dictionary in Django templates

Change

{% for key, log in log_group %}

to

{% for key, log in log_group.items %}

django template loop collections.defaultdict(lambda: collections.defaultdict(list))

My view.py solution was:

    my_dict = collections.defaultdict(lambda: collections.defaultdict(list))

for obj in queryset:
my_dict[int(obj.position.split('-')[0])][int(obj.position.split('-')[2])].append(obj)

for obj in my_dict:
my_dict[obj].default_factory = None

return Response({'queryset': dict(my_dict)}, template_name='_internal_template.html')

Linked to https://code.djangoproject.com/ticket/16335 and
Django template can't loop defaultdict

My template

{% for groups in queryset.itervalues  %}

groups = {{ groups }}
<br><br>

{% for cols in groups.itervalues %}

cols = {{ cols }}
<br><br>

{% for obj in cols %}

obj = {{ obj}} in <br><br>
obj info = {{ obj.title }}, {{ obj.abstract }}<br>

{% endfor %}

{% endfor %}

{% endfor %}

Django Dict of List Template

use this:

dict(dict_)

django template doesn't accept defaultdict

Django dict key,value in for loop does not work

You probably need to access .items() of the dict that you called errors. Just iterating over a dict gives you the keys, but not the values.

You can change your code to:

{% for k, v in errors.items %}
<div class="{{ v.class }}"><p>{{ v.txt }}</p></div>
{% endfor %}

Of course, if you don't need the keys (if and else) then you could also use .values() instead of items() to just get the values inside the dict.



Related Topics



Leave a reply



Submit