how to split the string in django template?

Akhi picture Akhi · Jan 30, 2017 · Viewed 17.7k times · Source

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

Answer

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