Search code examples
pythondjango-templatesdjango-filterdjango-1.5

how to split the string in django template?


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


Solution

  • Split is a custom filter, don't forget to create your filter, and to load it in your HTML page. Documentation for Django 4.0: https://docs.djangoproject.com/en/4.0/howto/custom-template-tags/

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