【问题标题】:How to access the dynamic key in Django template? [duplicate]如何访问 Django 模板中的动态键? [复制]
【发布时间】:2018-10-05 19:47:00
【问题描述】:

请看以下代码:

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

{{row.1.col}} 中,col 可以是任何值,例如 NAMEPHONE 等。当我像 {{row.1.PHONE}} 一样访问它时,我会在 html 中获得值,但是当我像 @987654327 一样访问它时@html 中不显示任何内容。

【问题讨论】:

  • 第二个 for 循环似乎是错误的。行的结构是什么?

标签: django django-templates


【解决方案1】:

你不能那样访问它,djangos 模板语言不允许那样。请参阅@BearBrown 在他的评论中提到的this post

你可以自己写custom template filter 喜欢this answer 显示:

from django.template.defaulttags import register
...
@register.filter
def get_item(dictionary, key):
    return dictionary.get(key)

【讨论】:

    猜你喜欢
    • 2012-06-07
    • 2016-06-18
    • 2012-07-17
    • 2013-06-01
    • 1970-01-01
    • 2021-04-10
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多