Opened 18 years ago

Closed 18 years ago

Last modified 18 years ago

#1495 closed enhancement (wontfix)

template variable dictionary lookups use arguments as strings not as variable names

Reported by: gt-django@… 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


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"}}

"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 Adrian Holovaty, 18 years ago

Resolution: wontfix
Status: newclosed

This is a bit out of the scope of the template system at this point.

comment:2 by gt-django@…, 18 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. {{ }}
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" )


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