1

Consider these two dictionaries:

a = {'f': 'foo'}
b = {'bar': 20,
     'foo': 10}

In Python, should I want to use the value of one dictionary as a key to another, I can do:

print(b[a['f']])
>>>10

However, in Django, I haven't been able to implement the same. I have tried using these:

{{ b.{{a.f}} }}

which result in

Could not parse the remainder: '{{a.f from 'b.{{a.f'

Similarly I have tried using these:

{{ b.{{a['f']}} }}
{{ b[a['f']] }}
{{ b[a.f] }}

None of them seem to work.

What should I do to implement the above said python code in Django Template Language

  • 4
    This is not possible (or at least not without using (custom) template filters, etc.). Django templates deliberately implement a restricted version, since you should implement such logic in the *view*, not in the template. – Willem Van Onsem Oct 24 '18 at 09:30
  • You might want to look into switching your Django template system to Jinaj2, which is roughly based on Django template language but allows for much more freedom as a programming language. – blhsing Oct 24 '18 at 09:44

0 Answers0