Opened 10 years ago

Closed 10 years ago

Last modified 10 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
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: UI/UX:


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 Changed 10 years ago by adrian

  • Resolution set to wontfix
  • Status changed from new to closed

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

comment:2 Changed 10 years ago by gt-django@…

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