我正在寻找一种快速的方法来从模板

时间:2017-05-04 21:10:16

标签: python django python-3.x django-templates

我有以下情况:

{% if accounts.count > 0 %}
    {% for account in accounts %}
      <div>Balance:<b>{{ my_dict.account.id }}</b></div>
      <div><a href="/edit/{{ account.id }}"><button>Edit</button></a></div>
    {% endfor %}
{% else %}
    <p>...</p>
{% endif %}

这样的安排没问题

my_dict.account

但我会做那样的事情。我有两个点

my_dict.account.id

有没有办法做到这一点?在模板中创建变量是个好主意,但它对我不起作用

1 个答案:

答案 0 :(得分:0)

很高兴看到你的my_dict字典结构:什么是关键(它是帐号id?)

可能是这样的:

my_dict[account.id]

虽然如果my_dict以某种方式没有密钥,你会遇到错误。所以你可能想要这样做:

my_dict.get(account.id, 'default value')
如果my_dict中没有这样的键,

'默认值'将返回。