#1495 closed enhancement (wontfix)
template variable dictionary lookups use arguments as strings not as variable names
Reported by: | Owned by: | Adrian Holovaty | |
---|---|---|---|
Component: | Template system | Version: | |
Severity: | normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I was looking at trying to do something in a template like:
{% for obj in object_list %}
{{ ratings_dict.obj }}
But it didn't work. Testing shows that my problem is that the template system doesn't do a dictionary lookup of the VARIABLE obj, but rather of the string "obj".
The docs show an example:
from django.core.template import Context, Template
t = Template("My name is {{ person.first_name }}.")
d = {"person": {"first_name": "Joe", "last_name": "Johnson"}}
t.render(Context(d))
"My name is Joe."
Perhaps you might want to first attempt to resolve the variable "first_name" (as in the above example) and then if it doesn't resolve, then treat it as a character string.
Or, since it would be easy to explicitly specify it AS a character string (e.g. {{ person."first_name" }} for those times when you want the string first_name instead of the variable first_name to be used, that might be an alternative implementation.
I don't know if this is a "defect" or an enhancement, but I'll opt for "enhancement" and let the developers decide.
Thank you
Change History (2)
comment:1 by , 19 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
comment:2 by , 19 years ago
ok... then please update the docs to make it clear when one uses template variables which parts are taken as literal strings and which parts are resolved as variables.
i.e. {{ alpha.bravo.delta.echo }}
Is it just "alpha" that's resolved as a variable? or is it dependent on which lookup succeeds?
(I still think that always resolving each part as a variable unless it's explicitly quoted text is better -- i.e. if when you want it, just say alpha."bravo",delta,"echo" )
Thanks
This is a bit out of the scope of the template system at this point.