Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the dynamic key in Django template? [duplicate]

Please see the following the code:

{% for row in df_src.iterrows %}
   <tr >
    <td><input type="checkbox"></td>                              
    {% for col in columns %}
      <td  class="redrow">{{row.1.col}}</td>                                  
    {% endfor %}                                                               
   </tr>
{% endfor %}

Here in {{row.1.col}} where col can be any value like NAME, PHONE, etc. When I access it like {{row.1.PHONE}} I get the value in html, however when I access it like {{row.1.col}} nothing is shown in html.

like image 631
Faizan Ali Avatar asked Nov 24 '25 10:11

Faizan Ali


1 Answers

You cannot access it that way, djangos template language does not allow that. See this post that @BearBrown mentioned in his comment.

You could write your own custom template filter like this answer shows:

from django.template.defaulttags import register
...
@register.filter
def get_item(dictionary, key):
    return dictionary.get(key)
like image 85
Ralf Avatar answered Nov 26 '25 23:11

Ralf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!