how to split the string in django template?

▼魔方 西西 提交于 2019-12-07 03:38:39

问题


i am trying to split the string in template using custom template filter. But i got an error

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

Here it is my filter

@register.filter(name='split')
def split(value, key):
    """
        Returns the value turned into a list.
    """
    return value.split(key)

this is my template

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

Thanks


回答1:


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



回答2:


For extract character string, use filter cut:

<a href="tel://+1{{ phone|cut:'-' }}">Phone</a>

this removes the scripts from the string.



来源:https://stackoverflow.com/questions/41932634/how-to-split-the-string-in-django-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!