[Django] #25574: A minor enhancement to "for" tag description in the reference

14 views
Skip to first unread message

Django

unread,
Oct 20, 2015, 6:12:37 AM10/20/15
to django-...@googlegroups.com
#25574: A minor enhancement to "for" tag description in the reference
-------------------------+-------------------------------------------------
Reporter: wodny | Owner: nobody
Type: | Status: new
Uncategorized |
Component: | Version: 1.8
Documentation | Keywords: for tag lookup order dict
Severity: Normal | defaultdict
Triage Stage: | Has patch: 0
Unreviewed |
Easy pickings: 0 | UI/UX: 0
-------------------------+-------------------------------------------------
I would like to propose a minor enhancement to a
[https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#for
paragraph] which gives an example on using the "for" tag with
dictionaries:
{{{#!django
{% for key, value in data.items %}
{{ key }}: {{ value }}
{% endfor %}
}}}
Currently it doesn't recall the reader that the lookup order would cause
different output if the "dict" dictionary contains the "items" key than
when it doesn't.

On the other hand a paragraph about defaultdict on
[https://docs.djangoproject.com/en/1.8/ref/templates/language/#variables
another page] suggests using dict instead of defaultdict. But while it
solves the problem of creating an entry by making a lookup it doesn't
solve the problem of "items" key within a dictionary.

I thought that maybe one or both pages could contain a reminder that the
view author should remember not to pass a dictionary with the "items" key
if the template author wants to use that "for key, value" construct.

--
Ticket URL: <https://code.djangoproject.com/ticket/25574>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 20, 2015, 8:48:11 AM10/20/15
to django-...@googlegroups.com
#25574: A minor enhancement to "for" tag description in the reference
-------------------------------------+-------------------------------------

Reporter: wodny | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Documentation | Version: 1.8
Severity: Normal | Resolution:
Keywords: for tag lookup | Triage Stage: Accepted
order dict defaultdict |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_better_patch: => 0
* needs_tests: => 0
* easy: 0 => 1
* needs_docs: => 0
* type: Uncategorized => Cleanup/optimization
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/25574#comment:1>

Django

unread,
Oct 20, 2015, 12:38:11 PM10/20/15
to django-...@googlegroups.com
#25574: A minor enhancement to "for" tag description in the reference
-------------------------------------+-------------------------------------
Reporter: wodny | Owner: jbzdak
Type: | Status: assigned

Cleanup/optimization |
Component: Documentation | Version: 1.8
Severity: Normal | Resolution:
Keywords: for tag lookup | Triage Stage: Accepted
order dict defaultdict |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jbzdak):

* owner: nobody => jbzdak
* status: new => assigned


Comment:

If this is OK (I don't really know what is the protocol) I'll do this
issue.

--
Ticket URL: <https://code.djangoproject.com/ticket/25574#comment:2>

Django

unread,
Oct 20, 2015, 1:37:44 PM10/20/15
to django-...@googlegroups.com
#25574: A minor enhancement to "for" tag description in the reference
-------------------------------------+-------------------------------------
Reporter: wodny | Owner: jbzdak
Type: | Status: assigned
Cleanup/optimization |
Component: Documentation | Version: 1.8
Severity: Normal | Resolution:
Keywords: for tag lookup | Triage Stage: Accepted
order dict defaultdict |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jbzdak):

* has_patch: 0 => 1


Comment:

Here is the PR link: https://github.com/django/django/pull/5450.

Here is full wording (maybe a bit verbose):
{{{
Keep in mind that ``items`` key in a dictionary will shadow
``items()``
function. This lookup order can cause some unexpected behavior.

In normal situations Django template expression ``{{ data.items }}``
will
return result of following python code: ``data.items()``, however if
``data`` dictionary will contain ``items`` key, the same django
expression
will return: ``data['items']``, which is most probably *not what you
want*.

You should avoid adding dictionary entries whose keys are named like
methods
of dictionary instance for example: ``items``, ``values``, ``keys``,
to
dictionaries that will be passed to Django templates.

For additional information see
:ref:`documentation of template variables <template-variables>`.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25574#comment:3>

Django

unread,
Oct 21, 2015, 11:44:30 AM10/21/15
to django-...@googlegroups.com
#25574: A minor enhancement to "for" tag description in the reference
-------------------------------------+-------------------------------------
Reporter: wodny | Owner: jbzdak
Type: | Status: closed

Cleanup/optimization |
Component: Documentation | Version: 1.8
Severity: Normal | Resolution: fixed

Keywords: for tag lookup | Triage Stage: Accepted
order dict defaultdict |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"32cd7069711d2e02fcd20c005c59c175d542c62e" 32cd706]:
{{{
#!CommitTicketReference repository=""
revision="32cd7069711d2e02fcd20c005c59c175d542c62e"
Fixed #25574 -- Documented {{ dict.items }} shadowing in for template tag
docs.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25574#comment:4>

Django

unread,
Oct 21, 2015, 11:46:20 AM10/21/15
to django-...@googlegroups.com
#25574: A minor enhancement to "for" tag description in the reference
-------------------------------------+-------------------------------------
Reporter: wodny | Owner: jbzdak
Type: | Status: closed
Cleanup/optimization |
Component: Documentation | Version: 1.8
Severity: Normal | Resolution: fixed
Keywords: for tag lookup | Triage Stage: Accepted
order dict defaultdict |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"5a9e93e05423c176adfef714f804fe80e6fbb522" 5a9e93e0]:
{{{
#!CommitTicketReference repository=""
revision="5a9e93e05423c176adfef714f804fe80e6fbb522"
[1.9.x] Fixed #25574 -- Documented {{ dict.items }} shadowing in for
template tag docs.

Backport of 32cd7069711d2e02fcd20c005c59c175d542c62e from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25574#comment:5>

Django

unread,
Oct 21, 2015, 11:46:20 AM10/21/15
to django-...@googlegroups.com
#25574: A minor enhancement to "for" tag description in the reference
-------------------------------------+-------------------------------------
Reporter: wodny | Owner: jbzdak
Type: | Status: closed
Cleanup/optimization |
Component: Documentation | Version: 1.8
Severity: Normal | Resolution: fixed
Keywords: for tag lookup | Triage Stage: Accepted
order dict defaultdict |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"3e8234b77e2698d14a7825319845e5038424f09d" 3e8234b]:
{{{
#!CommitTicketReference repository=""
revision="3e8234b77e2698d14a7825319845e5038424f09d"
[1.8.x] Fixed #25574 -- Documented {{ dict.items }} shadowing in for
template tag docs.

Backport of 32cd7069711d2e02fcd20c005c59c175d542c62e from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/25574#comment:6>

Reply all
Reply to author
Forward
0 new messages