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

岁酱吖の 提交于 2019-12-20 04:53:22

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/50020473/how-to-access-the-dynamic-key-in-django-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!