Explanation:
That template filters is util for combine boolean-return conditions using template tag {% if %}
.
Setup:
Insert the snippet into an_app/templatetags/myutils.py.
Use in template: {% load myutils %} and use filters as following:
{% if 10|is_multiple_of:5 %}
{% if 10|in_list:a_list_variable %}
{% if user.username|is_equal:a_variable %}
{% if user.username|is_not_equal:a_variable %}
{% if a_variable_|is_lt:5 %}
{% if a_variable_|is_lte:5 %}
{% if a_variable_|is_gt:5 %}
{% if a_variable_|is_gte:5 %}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | from django.template import Library
register = Library()
@register.filter
def multiple_of(value,arg):
return value % arg == 0
@register.filter
def in_list(value,arg):
return value in arg
@register.filter
def is_equal(value,arg):
return value == arg
@register.filter
def is_not_equal(value,arg):
return value != arg
@register.filter
def is_lt(value,arg):
return int(value) < int(arg)
@register.filter
def is_lte(value,arg):
return int(value) <= int(arg)
@register.filter
def is_gt(value,arg):
return int(value) > int(arg)
@register.filter
def is_gte(value,arg):
return int(value) >= int(arg)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 9 months ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 9 months, 1 week ago
- Serializer factory with Django Rest Framework by julio 1 year, 4 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 4 months ago
- Help text hyperlinks by sa2812 1 year, 5 months ago
Comments
Please login first before commenting.