Hi I am using App Engine/Python to do a simple website. I have some trouble with a Django template problem.
In short, I want to use a “ShortName” to access a “LongName”.
The soource code:
LongName={"so":"stackoverflow","su":"superuser"}
ShortName=['so','su']
Then I pass these two parameters to the templates.
In the template I write:
{% for aname in ShortName %}
{{ aname }} stands for {{ LongName.aname }},
{% endfor %}
The output is:
so stands for, su stands for
No error is given. The LongName.aname wont work.
I have no idea whats wrong.
This is trying to access
LongName['aname'], notLongName[aname].You might have to write a custom template tag/filter to get this to work. This Django bug (marked WONTFIX) has a simple implementation:
which you would use by
after adding it to your app (that SO answer shows how to do it on GAE).
You could also pre-make a variable to loop over in the view, by passing in
If you really don’t want to add a template tag — which isn’t that bad! you just make one file! 🙂 — or pass in an extra variable, Vic’s approach will let you do this without touching the Python files at all. As he mentions, it involves a lot of pointless iteration, but it’ll work fine for small lists.