0

I have some issues with access of a specific key in my template. Here is my code :

{% for rsim,couples in zipped_formated_rxnsim_pairedsim %}
    -{{rsim.xxx_fk.xxx_code_char}} #ex : toto
    {% for rsim2,couples2 in mydict.{{rsim.xxx_fk.xxx_code_char}}%}
        <li> {{rsim2}} - {{couples2}}</li>
    {% endfor %}
{% endfor %}

It gives me this error,

Could not parse the remainder: '{{rsim.xxx_fk.xxx_code_char}}' from 'mydict.{{rsim.xxx_fk.xxx_code_char}}'

I do not understand this behaviour, because if I hard code the line {% for rsim2,couples2 in mydict.{{rsim.pdb_fk.pdb_code_char}}%} to {% for rsim2,couples2 in mydict.toto %} it works ... I mean, how can I do something about this idea ?

I really need this loop structure, because I want to go through "rsim2,couples2" tuples only for a specific "rsim,couple" tuple. In others words, "rsim2,couples2", are extra data linked with "rsim,couples". What a better solution than a dictionary using a key ?

I tried using the {{with}} tag, adding a variable to use directly as a key, but it did not work. Any help would be appreciated.

Thank you !

Abijith Mg
  • 2,647
  • 21
  • 35
C.Brn
  • 143
  • 1
  • 1
  • 11
  • What's not to understand? You can't use `{{ }}` variable syntax when you're already inside a template tag. – Daniel Roseman Apr 11 '17 at 09:17
  • By not understanding, I meant, how can I do something similar. Thank you @DanielRoseman – C.Brn Apr 11 '17 at 09:19
  • Possible duplicate of [Django template how to look up a dictionary value with a variable](http://stackoverflow.com/questions/8000022/django-template-how-to-look-up-a-dictionary-value-with-a-variable) – Mateusz Knapczyk Apr 11 '17 at 09:45
  • @MateuszKnapczyk I saw it before asked my question, I wanted others ( better ? ) solutions rather write a custom template filter. You can see that others answers about your link do not answer to the original question. – C.Brn Apr 11 '17 at 10:05

1 Answers1

0

General answers are

  1. Add a helper method to accomplish what you want in Python rather than template language. This is usually recommended if in one's template, thingy.something refers to a Django model instance. Here, it doesn't appear to.

  2. Write your own template tag or filter, or search for an appropriate one that someone else has already written. Writing a template tag such as this is actually quite easy -- just follow the example in the Django doc. [edit] See the link posted in the question's comments.

  3. Use a different template language to the Django built-in one. Django now supports Jinja2. I don't know if that can do what you want, and have no experience with using it. It's probably using a sledgehammer to crack a nut if the above is the only problem you have with Django's own template language.

nigel222
  • 7,582
  • 1
  • 14
  • 22