在Django模板中的if标签中使用两个变量
大家好,我不太确定怎么清楚地解释这个,但我试试……
我需要用两个变量,像这样:
{% for client in clients %}
{% if user.client.username %}
我需要{% if user.username %},但是username的值是在循环中的client.username。
有没有办法做到这一点呢?
2 个回答
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
如果我理解得没错,user
是一个字典,你想在每次循环中查找由 client
索引的值,比如在 Python 中用 user[client].username
来获取。
不过,这在 Django 模板中是故意不允许的,因为这个语言比较简单,目的是为了让你在代码中先处理好数据。
所以,你应该在把数据传给模板之前,把两个列表或字典合并在一起。