#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)
Change History (13)
comment:1 by , 14 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 14 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
by , 14 years ago
Attachment: | 13608.diff added |
---|
comment:3 by , 14 years ago
Has patch: | set |
---|
Patch attached with changes for the templates topics page and template API reference.
comment:4 by , 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:6 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → Cleanup/optimization |
by , 12 years ago
Attachment: | 13608.2.diff added |
---|
comment:10 by , 12 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
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.