Opened 5 years ago

Closed 3 years ago

Last modified 3 years ago

#13608 closed Cleanup/optimization (fixed)

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

Reported by: anonymous Owned by: dwillis
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

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 dwillis 5 years ago.
13608.2.diff (1.6 KB) - added by timo 3 years ago.

Download all attachments as: .zip

Change History (13)

comment:1 Changed 5 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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 5 years ago by dwillis

  • Owner changed from nobody to dwillis
  • Status changed from new to assigned

Changed 5 years ago by dwillis

comment:3 Changed 5 years ago by dwillis

  • Has patch set

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

comment:4 Changed 5 years ago by gabrielhurley

  • 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 5 years ago by dwillis

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

comment:6 Changed 4 years ago by julien

  • Severity set to Normal
  • Type set to Cleanup/optimization

comment:7 Changed 3 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:8 Changed 3 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

Changed 3 years ago by timo

comment:9 Changed 3 years ago by timo

  • Patch needs improvement unset

Added new patch for consideration

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

  • Resolution set to fixed
  • Status changed from assigned to closed

In [74c025d0285450bf277afbee095172af54562ab6]:

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

comment:11 Changed 3 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