1

I'm having a problem with django. I have a dict with all my site texts for translations. For example:

term = {"level_1": "Noob",
        "level_2": "Noob 2"}

The problem is, how can I access this key on django template?

I have

img src="/images/level_{{player.level.id}}.jpg" 
    title="{{term.level??????? }}"

I tried:

title="{{term.level{{player.level.id}}}} 

but of course this didn't work.

Reinout van Rees
  • 13,486
  • 2
  • 36
  • 68
Rodrigo
  • 188
  • 1
  • 8
  • 1
    possible duplicate of [Accessing a dict by variable in Django templates?](http://stackoverflow.com/questions/2067006/accessing-a-dict-by-variable-in-django-templates) – David Wolever Nov 14 '12 at 21:50

1 Answers1

1

Django's template language is (by design) pretty dumb/restricted. In his comment, Davind Wolever points at Accessing a dict by variable in Django templates?, where an answer suggests to make a custom template tag.

I think that in your case, it is best to handle it in your view code. Instead of only passing along a player into your context, pass both the level ID and the level name.

Possibly you can even directly pass the image url and the level name? Not constructing the URL in your template makes it more readable.

Community
  • 1
  • 1
Reinout van Rees
  • 13,486
  • 2
  • 36
  • 68