Opened 14 years ago

Closed 12 years ago

Last modified 12 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: dev
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

Description

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: foo.bar
    * Method call. Example: foo.bar()
    * List-index lookup. Example: foo[bar]

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

View...

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

Template...

{% 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 14 years ago.
13608.2.diff (1.6 KB ) - added by Tim Graham 12 years ago.

Download all attachments as: .zip

Change History (13)

comment:1 by Russell Keith-Magee, 14 years ago

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 by Derek Willis, 14 years ago

Owner: changed from nobody to Derek Willis
Status: newassigned

by Derek Willis, 14 years ago

Attachment: 13608.diff added

comment:3 by Derek Willis, 14 years ago

Has patch: set

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

comment:4 by Gabriel Hurley, 14 years ago

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 by Derek Willis, 14 years ago

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

comment:6 by Julien Phalip, 14 years ago

Severity: Normal
Type: Cleanup/optimization

comment:7 by Aymeric Augustin, 13 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:8 by Aymeric Augustin, 13 years ago

Easy pickings: unset

Change Easy pickings from NULL to False.

by Tim Graham, 12 years ago

Attachment: 13608.2.diff added

comment:9 by Tim Graham, 12 years ago

Patch needs improvement: unset

Added new patch for consideration

comment:10 by Tim Graham <timograham@…>, 12 years ago

Resolution: fixed
Status: assignedclosed

In [74c025d0285450bf277afbee095172af54562ab6]:

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

comment:11 by Tim Graham <timograham@…>, 12 years ago

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