Combining a constant value and a variable for the default value of a template

I am setting up a template in which I would like the default value to be a combination of a constant string and a variable value. The desired HTML output would look something like this:

<span id="id1234" class="foo"> Click <a href="/images/img1234.jpg">here to view image</a>. </span> 

In the template code for span I would like something like:

 <span id="{{ spanid|default:'id'object.id }}" class="foo"> 

Similarly, the a tag will use:

 <a href="/images/{{ image_file|default:'img'object.id'.jpg'"> here to view... 

This does not work, is there a way to do this in the syntax of the django templates and the default filter?

+4
source share
1 answer

You can not. However, default is just a shortcut. In this case, the shortcut does not work, but a longer form will allow you to do what you need:

 {% if image_file %}{{ image_file }}{% else %}img{{ object.id }}.jpg{% endif %} 
+4
source

Source: https://habr.com/ru/post/1388629/


All Articles