Search code examples
pythondjangotemplatesdjango-template-filters

Django 3 Problem with concatenating inside a template tag


I want to concatenate a string inside a template in Django (version 3). I have read everything I could find on the subject and found that this should be the way:

{{ "ST" | add: item.id | stringformat:"08d" }}

But it produces the error:

django.template.exceptions.TemplateSyntaxError: add requires 2 arguments, 1 provided

Can anybody shed light into my darkness?


Solution

  • As @Willem Van Onsum points out the id (pk) is an integer and has to be converted to a string before concatenating. Besides the spaces have to be removed for it to work. See: TemplateSyntaxError: 'with' expected with atleast one variable assignment

    Hence the solution is:

        {% with n=item.id|stringformat:"08d" %}
            {{ "ST"|add:n }}
        {% endwith %}
    

    Also thanks to @jaswanth for his contribution.