如何在Django模板中访问动态密钥? [英] How to access the dynamic key in Django template?

查看:68
本文介绍了如何在Django模板中访问动态密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参见以下代码:

{% 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 %}

{{行。 1.col}} ,其中 col 可以是任何值,例如 NAME PHONE 等。当我像 {{row.1.PHONE}} 一样访问它时,我会在html中获得该值,但是当我像 {{row.1.col}} 这样访问它。html中什么都没显示。

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.

推荐答案

您不能以这种方式访问​​它,djangos模板语言不允许这样做。请参阅这篇文章, @bearBrown在他的评论中提到。

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)

这篇关于如何在Django模板中访问动态密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆