I’m using Django 1.3. If I put the following fragment into my template:
{% if 'my string'|length > 10 %}{{ 'my string'|length }}{% endif %}
the rendering engine prints ‘9’. The only thing I can think of is that the |length filter is returning a string, but that seems odd in the extreme. Can anyone point me in the right direction?
Thanks!
Edit:
The length I actually want to test comes from flatpage.title provided by django.contrib.flatpages. For this reason, I’d rather not hack the view to provide the information I need to the template. I’d hoped I could simply use the |length filter as described in the Django docs, here. However, as has been pointed out, the only way to do this seems to be to also use the |get_digit filter, whose behaviour is not clearly defined in this respect. 🙁
Yes, filters always return a string.
You can achive the desired functionality by calculating string length in a view and do something like this:
Or create a custom filter for your needs: http://code.djangoproject.com/wiki/BasicComparisonFilters
Edited for typo