Well, it does, except that when you do a default_dict.items, it adds "items" to the dict with a value of 0 in the template. To verify this, try dumping default_dict after the exception is thrown. You will get a new key called "items".
To fix your problem, pass dict(default_dict) to the Context. >>> t.render(Context({'data': dict(default_dict)})) -- Raja On Nov 7, 12:51 pm, Hyungyong Kim <[EMAIL PROTECTED]> wrote: > Hi, > > Since a few hours ago, I've suffered following problem. > I found this problem is due to collections.defaultdict. > > Normal dictionary is working collectly, but defaultdict is not > working. > > >>> from django.template import Template, Context > >>> t = Template("{% for k,v in data.items %}{{ k }}: {{ v }}{% endfor %}") > >>> normal_dict = {'a':1, 'b':2} > >>> t.render(Context({'data':normal_dict})) > u'a: 1b: 2' > > >>> from collections import defaultdict > >>> default_dict = defaultdict(int) > >>> default_dict[1]+=1 > >>> default_dict[2]+=1 > >>> default_dict[1]+=1 > >>> default_dict > > defaultdict(<type 'int'>, {1: 2, 2: 1})>>> > t.render(Context({'data':default_dict})) > > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > File "/usr/lib/python2.5/site-packages/django/template/__init__.py", > line 176, in render > return self.nodelist.render(context) > File "/usr/lib/python2.5/site-packages/django/template/__init__.py", > line 768, in render > bits.append(self.render_node(node, context)) > File "/usr/lib/python2.5/site-packages/django/template/debug.py", > line 81, in render_node > raise wrapped > django.template.TemplateSyntaxError: Caught an exception while > rendering: 'int' object is not iterable > > Original Traceback (most recent call last): > File "/usr/lib/python2.5/site-packages/django/template/debug.py", > line 71, in render_node > result = node.render(context) > File "/usr/lib/python2.5/site-packages/django/template/ > defaulttags.py", line 122, in render > values = list(values) > TypeError: 'int' object is not iterable > > > > I think defaultdict have to work as normal dict. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---