如何拆分 Django 模板中的字符串?
how to split the string in django template?
我正在尝试使用自定义模板过滤器拆分模板中的字符串。但是我得到一个错误
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 %}
提取字符串,使用过滤切割:
<a href="tel://+1{{ phone|cut:'-' }}">Phone</a>
这将从字符串中删除脚本。
直接for循环也可以,你只需要删除语法中的空格:
<h4>Skills</h4>
{% for skill in form.instance.skills|split:"," %}
{{ skill }}
{% endfor %}
我正在尝试使用自定义模板过滤器拆分模板中的字符串。但是我得到一个错误
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 %}
提取字符串,使用过滤切割:
<a href="tel://+1{{ phone|cut:'-' }}">Phone</a>
这将从字符串中删除脚本。
直接for循环也可以,你只需要删除语法中的空格:
<h4>Skills</h4>
{% for skill in form.instance.skills|split:"," %}
{{ skill }}
{% endfor %}