1

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.

Susan Mayer
  • 335
  • 1
  • 3
  • 12
  • 1
    It looks to me like your declaration of LongName has an unneeded and unmatched single-quote. Is that actually in your code, or is it just a typo in your question? – Adam Crossland Apr 28 '12 at 18:06

2 Answers2

5

This is trying to access LongName['aname'], not LongName[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:

def get(d, key):
    return d.get(key, '')

register.filter(get)

which you would use by

{{ LongName|get:aname }}

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

 # in view
 name_abbrevs = [(k, LongName[k]) for k in ShortName]

 # in template
 {% for short_name, long_name in name_abbrevs %}
     {{ short_name }} stands for {{ long_name }}
 {% endif %}

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.

Community
  • 1
  • 1
Danica
  • 28,423
  • 6
  • 90
  • 122
  • 1
    Thanks a lot! The idea of adding something to the source code just scares me. I have no experience of messing the source code. – Susan Mayer Apr 28 '12 at 18:27
  • Did you really edit your answer to use my answer? You need to find a better way to spend your weekend. – Vic Fryzel Apr 28 '12 at 18:53
  • @VicFryzel Huh? My edited additions don't use your approach (no nested loops). I do obviously link to your answer at the end and talk about how it relates to the other approaches I suggested. – Danica Apr 28 '12 at 18:56
  • @Dougal, the for loop and the if. Everything below the horizontal rule uses my approach. Susan also left a comment saying she wanted to avoid messing with the Python code, and yet you still gave her an answer for which she'd have to modify the code. – Vic Fryzel Apr 28 '12 at 19:01
  • @VicFryzel This is probably the least important dispute in the history of the internet, but my second listed approach doesn't use `ShortName`. I suppose that the version I suggested using `if sname in ShortName` is pretty much the same as yours with the loop order switched, though I didn't realize that as I was writing it. The third approach (adding the variable to the view) is different, and although it does involve modifying the Python, it's a pretty simple modification. In any case, if Susan doesn't want to touch the Python, she's free to use your approach (which I upvoted, incidentally). – Danica Apr 28 '12 at 19:12
  • @Dougal, more realistically, this is a somewhat fruitless debate about the lack of merit in a person stealing another's work for personal gain. The history of the internet goes back a long way, after all. Certainly, since its inception, there have been less significant disputes. However, while engaged in this type of behavior, you must come across debates of similar significance more often than others. – Vic Fryzel Apr 28 '12 at 19:21
  • @VicFryzel, I think it's going a little far to call this "stealing for personal gain". In any case, I've edited my answer to take out the stuff that you think is rightfully your approach, and now I'm done with this unless Susan comes back with more questions. – Danica Apr 28 '12 at 19:26
  • @Dougal, Thanks for removing some of the stolen bits. You're a stand-up person. – Vic Fryzel Apr 28 '12 at 19:29
2

Django templates have a drawback here. I've been in the same situation before. What you have to do is iterate over all the keys in LongName, and check if the key you're looking for matches the ShortName. Here you go:

{% for aname in ShortName %}
  {% for short_version, long_version in LongName %}
    {% if aname == short_version %}
      {{ aname }} stands for {{ long_version }},
    {% endif %}
  {% endfor %}
{% endfor%}

It's inefficient, and essentially a pointless O(n^2) mechanism. However, there's no better way in pure Django templates to refer to entries of a dict by a variable name.

Vic Fryzel
  • 2,395
  • 16
  • 20