Opened 8 years ago

Closed 6 years ago

Last modified 6 years ago

#13608 closed Cleanup/optimization (fixed)

Clarify failure of lookups when argument to lookup is not an int

Reported by: anonymous Owned by: Derek Willis
Component: Documentation Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


The docs for templating api say the following:

Dots have a special meaning in template rendering. A dot in a variable name signifies lookup. Specifically, when the template system encounters a dot in a variable name, it tries the following lookups, in this order:

    * Dictionary lookup. Example: foo["bar"]
    * Attribute lookup. Example:
    * Method call. Example:
    * List-index lookup. Example: foo[bar]

From this, I was under the impression that the following would work...


context = dict(sizes=['SM', 'MD', LG'], available= {'SM': True, 'MD' : False, 'LG' : False})
return render_to_response('index.html', context)


{% for size in sizes %}
   {% if apparel.size %}
       {{size}} is available!
   {% else %}
       {{size}} is not available!
   {% endif %}
{% endfor %}

I had to look into the code to discover that when the docs mean list-lookup foo[bar], that if bar is a string, it will not do a dictionary lookup. Clarifying that the list lookup will only work with integers would be helpful.

Attachments (2)

13608.diff (1.4 KB) - added by Derek Willis 8 years ago.
13608.2.diff (1.6 KB) - added by Tim Graham 6 years ago.

Download all attachments as: .zip

Change History (13)

comment:1 Changed 8 years ago by Russell Keith-Magee

Triage Stage: UnreviewedAccepted

To clarify the complaint: The issue here is that the list example foo[bar] implies that the list will do a lookup using the value of the variable 'bar', when it will in fact only work with foo.3 (or other integer value).

Given that variable lookup is an important omission from Django's template language, this warrants clarification.

comment:2 Changed 8 years ago by Derek Willis

Owner: changed from nobody to Derek Willis
Status: newassigned

Changed 8 years ago by Derek Willis

Attachment: 13608.diff added

comment:3 Changed 8 years ago by Derek Willis

Has patch: set

Patch attached with changes for the templates topics page and template API reference.

comment:4 Changed 8 years ago by Gabriel Hurley

Patch needs improvement: set

It looks to me like this patch slightly missed the mark. The issue isn't regarding list index lookup (lists are always indexed by integer value), it's regarding the template API's dot notation using the literal variable name for dictionary lookup rather than the variable's value.

Furthermore, it seems that the example given in the report is a case that ought to be covered in the docs showing how to use the {% for key, value in dict %} syntax:

{% for size, is_available in available %}
    {% if is_available %}
        do something here...
    {% else %}
        do something else
    {% endif %}
{% endfor %}

It's a commonly overlooked idiom in Django's template language (I've had two people ask me how to accomplish just that this week).

comment:5 Changed 8 years ago by Derek Willis

Alright, thanks. I'll take another stab at it.

comment:6 Changed 8 years ago by Julien Phalip

Severity: Normal
Type: Cleanup/optimization

comment:7 Changed 7 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:8 Changed 7 years ago by Aymeric Augustin

Easy pickings: unset

Change Easy pickings from NULL to False.

Changed 6 years ago by Tim Graham

Attachment: 13608.2.diff added

comment:9 Changed 6 years ago by Tim Graham

Patch needs improvement: unset

Added new patch for consideration

comment:10 Changed 6 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In [74c025d0285450bf277afbee095172af54562ab6]:

Fixed #13608 - Noted that template lookups use literal values.

comment:11 Changed 6 years ago by Tim Graham <timograham@…>

In [c2f1aa5a3c605927f1fb86c0d4eaa9f9067a0313]:

[1.4.x] Fixed #13608 - Noted that template lookups use literal values.

Backport of 74c025d0285450bf277afbee095172af54562ab6 from master.

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