Jay*_*ayd 5 django variables templates dictionary for-loop
我试图让以下工作.
count循环需要遍历所有值,并且可能没有与每个计数关联的用户,但是i需要在每个循环中使用计数值来传递给JavaScript.
python部分:
users = {}
users[1]={}
users[1][id]=...
users[1][email]=...
...
count=[1,2,3,4,5,6,7,8,9,10]
Run Code Online (Sandbox Code Playgroud)
Django模板部分:
{% for i in count %}
do some stuff with the value i using {{i}} which always returns the value, i.e. 1
email:{% if users.i.email %}'{{users.i.email}}'{% else %}null{% endif%}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
这不会返回任何电子邮件.当我代替数量1为i在{% if user.i.email %}电子邮件返回用户的电子邮件地址.我在JavaScript中使用数据,因此如果它不存在则需要隐式为null.我似乎无法让Django将i变量识别为变量而不是值i.
使用[]不起作用,因为它会抛出无效的语法错误
email:{% if users.[i].email %}'{{users.[i].email}}'{% else %}null{% endif%}
Run Code Online (Sandbox Code Playgroud)
我试过用" with"声明
{% for i in count %}{% with current_user=users.i %}...
Run Code Online (Sandbox Code Playgroud)
然后使用current_user.email,但一无所获
也试过了
{% for i in count %}{% with j=i.value %}...
Run Code Online (Sandbox Code Playgroud)
以防万一它会工作,然后尝试使用j,但结果相同.
我已经考虑过创建一个内部for循环来循环用户对象并检查我是否等于键/值,但这看起来很昂贵而且不是很可扩展.
任何想法我如何强制Django i作为变量查看并使用它的值作为索引,或任何其他方式来解决这个问题?
谢谢
Jayd
*编辑:
我按照Abhi的建议尝试了额外的for循环.
{% for i in count %}
{% for key, current_user in users.items %}
do some stuff with the value i using {{i}} which always returns the value, i.e. 1
email:{% if i == key and current_user.email %}'{{current_user.email}}'{% else %}null{% endif%}
{% endfor %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
这种工作,但现在它将重复do some stuff with the value i每个用户的价值.如果我输入一个if:
{% for i in count %}
{% for key, current_user in users.items %}
{% if i == key %}
do some stuff with the value i using {{i}} which always returns the value, i.e. 1
email:{% if i == key and current_user.email %}'{{current_user.email}}'{% else %}null{% endif%}
{% endif%}
{% endfor %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
当计数没有特定用户时,忽略循环.
我能看到的唯一方法就是在每个我想要使用的地方都有用户循环current_user.
{% for i in count %}
do some stuff with the value i using {{i}} which always returns the value, i.e. 1
email:{% for key, current_user in users.items %}{% if i == key and current_user.email %}'{{current_user.email}}'{% else %}null{% endif%}{% endfor %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
这看起来非常昂贵.有任何想法吗?
我在考虑编写一个过滤器,它返回用户使用的值i作为键:
{% with current_user=users|getuser:i %}
Run Code Online (Sandbox Code Playgroud)
但我不知道这是否会起作用或者我会得到相同的错误,其中i传递的值为'i'而不是变量名称.
我会试一试这么久.
*编辑
这没用.过滤器使用{{}}返回对象工作,但它在内部不起作用{% %}
.
感谢您的投入
正如Joe在评论中所说,字典显然是外容器的错误数据结构.你应该使用一个列表.
users = []
user = {'id':..., 'email': ...}
users.append(user)
Run Code Online (Sandbox Code Playgroud)
...
{% for user in users %}
{{ user.email }}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
如果您需要循环内每个用户的数量,您可以使用{{ forloop.counter }}.
我提出了以下解决方法:
这是过滤器代码:
@register.filter(name='dict_value_or_null')
def dict_value_or_null(dict, key):
if key in dict:
return dict[key]
else:
return 'null'
Run Code Online (Sandbox Code Playgroud)
这是模板代码
{% for i in count %}
do some stuff with the value i using {{i}} which always returns the value, i.e. 1
email:'{{users|dict_value_or_null:i|dict_value_or_null:'email'}}'
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
这很有效,所以我想这有我的解决方案。但我仍然认为,如果模板系统中有一种方法可以强制将 {% %} 内的值视为变量而不是文字,那么这一切可能会容易得多。
有没有办法做到这一点?
感谢您的输入