User User - 12 days ago 4
Python Question

Combine tags with filters

Code:

{% firstof m.caption m.altcaption|slice:":37" %}


I want to choose the first of those 2 variables, then slide to 37 characters. Any ideas?

Answer

In Django 1.9+ you can assign the result of the firstof tag to a variable.

{% firstof m.caption m.altcaption as caption %}
{{ caption|slice:":37"}} 

In earlier versions you could filter both arguments

{% firstof m.caption|slice:":37" m.altcaption|slice:":37" %} 
Comments