I’m setting up a template in which I’d like the default value to be a combination of a constant string and a variable id value. The desired HTML output would be something like:
<span id="id1234" class="foo">
Click <a href="/images/img1234.jpg">here to view image</a>.
</span>
In the template code for the span, I would like something like:
<span id="{{ spanid|default:'id'object.id }}" class="foo">
Similarly, the a tag would use:
<a href="/images/{{ image_file|default:'img'object.id'.jpg'"> here to view...
This doesn’t work, is there a way to do this within the syntax of django templates and the default filter?
You can’t. However,
defaultis merely a shortcut. In this scenario, the shortcut doesn’t work, but the longer form will allow you do what you need: