[英]Looping through a list/dictionary django template
my view.
teamname_list = team.objects.values('teamName')
in my template template html
{{teamname_list}}
outputs:
[{'teamName': "['Paul', 'John']"}]
---------------------------------------------------
alternatively values instead of values_list
view
teamname_list = team.objects.values('teamName')
in template
{{teamname_list}}
output:
[("['Paul', 'John']",)]
Is there any way to loop through teamname_list to get the characters individually so on the html page it shows as just Paul and John, treated as their own string or even producing a list so looping through them and adding them to an unordered list ie有没有办法循环遍历 teamname_list 以单独获取字符,因此在 html 页面上它显示为只是 Paul 和 John,将其视为自己的字符串,甚至生成一个列表,以便遍历它们并将它们添加到无序列表中,即
* paul
* john
Been trying for a while now couple of hours and can't seem to get anything to work, tried every possible solution except for filters because I seem to be having a problem with using them.已经尝试了一段时间,现在几个小时似乎无法得到任何工作,尝试了除过滤器之外的所有可能的解决方案,因为我似乎在使用它们时遇到了问题。 Any other solutions?
还有其他解决方案吗?
You can use a for loop in your django template.您可以在 Django 模板中使用for 循环。 For example:
例如:
<ul>
{% for teamname in teamname_list %}
<li>{{ teamname }}</li>
{% endfor %}
</ul>
EDIT: I didn't realize your list actually only has one value.编辑:我没有意识到您的列表实际上只有一个值。 Try doing this for starters:
尝试为初学者这样做:
teamname_list = team.objects.all()
teamname_list = team.objects.values_list('teamName', flat=True)
This will return list of names.这将返回名称列表。 Use one for loop in template and display names.
在模板和显示名称中使用一个 for 循环。
<ul>
{% for teamname in teamname_list %}
<li>*{{ teamname }}</li>
{% endfor %}
</ul>
teamname_list = team.objects.all()
HTML code HTML代码
<ul>
{% for name in teamname_list %}
<li>{{ name.teamName }}</li>
{% endfor %}
</ul>
Please share your model for more clarification请分享您的模型以获得更多说明
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.