gpt4 book ai didi

django-templates - 如何在Django模板中分割字符串?

转载 作者:行者123 更新时间:2023-12-04 14:55:34 24 4
gpt4 key购买 nike

我试图使用自定义模板过滤器拆分模板中的字符串。但是我有一个错误

    TemplateSyntaxError at /job/16/
'for' statements should use the format 'for x in y': for skill in form.instance.skills | split : ","

这是我的过滤器
@register.filter(name='split')
def split(value, key):
"""
Returns the value turned into a list.
"""
return value.split(key)

这是我的模板
<h4>Skills</h4>
{% for skill in form.instance.skills | split : "," %}
{{ skill }}
{% endfor %}

谢谢

最佳答案

<h4>Skills</h4>
{% with form.instance.skills|split:"," as skills %}
{% for skill in skills %}
{{ skill }}<br>
{% endfor %}
{% endwith %}

关于django-templates - 如何在Django模板中分割字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41932634/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com