Login

Basic logic filters

Author:
mikeivanov
Posted:
September 11, 2007
Language:
Python
Version:
.96
Score:
8 (after 8 ratings)

Usage:

{% if item|IN:list %}
  The item is in the list.
{% endif %}

{% if customer.age|LE:18 %}
  Go play out here.
{% endif %}

{% if product.price|add:delivery_cost|GT:balance %}
  Insufficient funds.
{% endif %}

You've got the idea.

Special thanks to guychi.

 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
from django import template
register = template.Library()

@register.filter
def EQ(value,arg): return value == arg

@register.filter
def LT(value,arg): return value < arg

@register.filter
def GT(value,arg): return value > arg

@register.filter
def LE(value,arg): return value <= arg

@register.filter
def GE(value,arg): return value >= arg

@register.filter
def NE(value,arg): return value != arg

@register.filter
def IS(value,arg): return value is arg

@register.filter
def IN(value,arg): return value in arg

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

james_027 (on December 28, 2007):

wow, this should be in the django official filter

#

Please login first before commenting.