Opened 17 years ago
Closed 17 years ago
#7373 closed (invalid)
for key, value in some_dict does not work
Reported by: | whoozle | Owned by: | nobody |
---|---|---|---|
Component: | Template system | Version: | dev |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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/defaulttags.py =================================================================== --- django/template/defaulttags.py (revision 7574) +++ django/template/defaulttags.py (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: nodelist.append(node.render(context)) if unpack:
Change History (4)
comment:1 by , 17 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:2 by , 17 years ago
It's hard to figure out from the current template documentation, sorry :)
Thank you!
comment:3 by , 17 years ago
Resolution: | invalid |
---|---|
Status: | closed → reopened |
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 by , 17 years ago
Resolution: | → invalid |
---|---|
Status: | reopened → closed |
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.
Use {% for field, error in form.error_dict.items %}