Login

is_in

Author:
Tomek
Posted:
December 16, 2010
Language:
Python
Version:
1.1
Score:
0 (after 0 ratings)

I know in Django 1.2 we may acquire the same result using {% if value in arg %} but I need this filter in Django 1.1.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
@register.filter
def is_in(value, arg):
    '''It performs the same action as Python in statement.
       It returns True if value is in arg, False if not or any error occur.
    
       For example:
       {{ 'abc'|is_in:'www.abc.com'}}   -> 'abc' in 'www.abc.com'
       
       x = 5
       l = [3,4,5,6]
       {{ x|is_in:l}                    -> x in l
    '''
    
    try:
        return value in arg
    except:
        return False

More like this

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

Comments

Please login first before commenting.