Opened 9 years ago

Closed 9 years ago

#7373 closed (invalid)

for key, value in some_dict does not work

Reported by: whoozle Owned by: nobody
Component: Template system Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


I have the following code in my template:

{% if form.has_errors %}
errors: <ol> {% for field, error in form.error_dict %}<li>{{ field }} <p><strong>{{ error }}</strong></p></li>{% endfor %}</ol>
{% endif %}

That stuff does not properly work as I believe it has to: all rows contains 'p' as 'field' and 'u' as 'error'.
Here's the patch:

Index: django/template/
--- django/template/      (revision 7574)
+++ django/template/      (working copy)
@@ -138,13 +138,14 @@
             # Boolean values designating first and last times through loop.
             loop_dict['first'] = (i == 0)
             loop_dict['last'] = (i == len_values - 1)
+            context[self.loopvars[0]] = item

             if unpack:
                 # If there are multiple loop variables, unpack the item into
                 # them.
-                context.update(dict(zip(self.loopvars, item)))
-            else:
-                context[self.loopvars[0]] = item
+                context[self.loopvars[1]] = values[item][0]
             for node in self.nodelist_loop:
             if unpack:

Change History (4)

comment:1 Changed 9 years ago by oyvind

Resolution: invalid
Status: newclosed

Use {% for field, error in form.error_dict.items %}

comment:2 Changed 9 years ago by anonymous

It's hard to figure out from the current template documentation, sorry :)
Thank you!

comment:3 Changed 9 years ago by whoozle

Resolution: invalid
Status: closedreopened

I reverted my changes(and svn update) and wrote
for field, error in form.error_dict.items
as you suggested, no success.

Now, I've got python list in error variable:

field: author 
error: [u'\u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u043b\u0435.']

I am django newbie. Do I have to add another inner loop to traverse through all errors or what ? I missed something in documentation, didn't I ?

comment:4 Changed 9 years ago by James Bennett

Resolution: invalid
Status: reopenedclosed

The error messages for a field are a list, because a single field may have multiple errors associated with it. This is covered in the forms documentation; please consult it for further information.

Note: See TracTickets for help on using tickets.
Back to Top