大家都不清楚如何清楚地解释这一点,但是这里...... [/ p>
我需要使用两个变量:
{% for client in clients %}
{% if user.client.username %}
我需要{%if user.username%},但username的值在循环中的client.username中。
有办法做到这一点吗?
答案 0 :(得分:1)
如果client.username
的值等于user.client.username
的值,您是否尝试执行某些操作?如果是这样,你想要:
{% if client.username == user.client.username %} # Works in Django 1.2 and above
{% ifequal client.username user.client.username %} # Works everywhere
答案 1 :(得分:1)
如果我理解正确,user
是一个dict,你想在循环的每次迭代中查找由client
索引的值 - 例如,Python中的user[client].username
。
这是(故意)在Django模板中是不可能的 - 语言是有限的,迫使你在代码中进行预处理。
相反,您应该将两个列表/ dicts压缩在一起,然后再将它们传递给模板。