[Fixed]-Django string concatenation inside template tag best practice


You can concatenate two strings in Django template as follows:

{{"First String "|add:"Second String"}}

Just replace the two strings with your own variable.


What I use when I want to concatenate strings in Django templates from variables (examples taken from my own code, tell me if you need something closer to your case):

<input id="myid_{{idBase}}_{{idFinal}}" type="checkbox"></input>

and inside a django tag, I use the keyword “add” associated with the keywork with

{% with 'images/'|add:file_name as image_static %}
     <img src="{% static image_static %}" title = "{{ tooltip }}"  alt = "{{ title }}"/>
{% endwith %}

Leave a comment