91
As of Django 1.2; if supports boolean operations and filters, so you can write this as:
{% if myarr|length > 1 %}
<!-- printing some html here -->
{% endif %}
See the Django Project documentation for if with filters.
7
no. but you can use django-annoying, and {% if myarr|length > 1 %}
will work fine.
- [Django]-Django Admin – Disable the 'Add' action for a specific model
- [Django]-Django admin: how to sort by one of the custom list_display fields that has no database field
- [Django]-Django template includes slow?
4
Sad, but there is no such functionality in django’s ‘if’ tag. There is a rumors that smarter if tag will be added in 1.2., at least it’s in High priority
list.
Alternatively you can use “smart_if” tag from djangosnippets.com
OR you can add your own filter (same like length_is filter) – but it’s just adding more useless code
from django import template
register = template.Library()
def length_gt(value, arg):
"""Returns a boolean of whether the value is greater than an argument."""
try:
return len(value) > int(arg)
except (ValueError, TypeError):
return ''
length_gt.is_safe = False
register.filter(length_gt)
For more info consult django docs
- [Django]-STATIC_ROOT vs STATIC_URL in Django
- [Django]-Django: Redirect to previous page after login
- [Django]-How to make Django's DateTimeField optional?
2
This is one of those powers the Django template language doesn’t give you. You have a few options:
-
Compute this value in your view, and pass it into the template in a new variable.
-
Install an add-on library of template tags that lets you get richer comparisons, for example: http://www.djangosnippets.org/snippets/1350/
-
Use a different templating language altogether, if you think you’ll be frequently running into templating language limitations.
- [Django]-Django: Can I create a QueryDict from a dictionary?
- [Django]-Django py.test does not find settings module
- [Django]-In Django Admin how do I disable the Delete link
0
Maybe this will be of any help ?
Checking collection sizes in Django templates is somewhat limited. The only solution that I was using, was passing another param from view to template – but to be honest, if depends from what You are trying to achieve.
- [Django]-Django column "name" of relation "django_content_type" does not exist
- [Django]-STATIC_ROOT vs STATIC_URL in Django
- [Django]-Empty Label ChoiceField Django