cleat_list

 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
36
37
38
39
@register.filter
def clear_list(list, clear=None):
    '''Template filters that clear list from unwanted elements, within django template. 
    
       Examples:
       
       l = [1, '1', '', ' ', None, 'www.abc.com']
       
       1)
       {{l|clear_list}} -> [1, '1', '', ' ','www.abc.com']
       
       2)
       {{l|clear_list:1}} -> ['1', '', ' ', None, 'www.abc.com']
       
       4)
       {{l|clear_list:'1'}} -> [1, '', ' ', None, 'www.abc.com']
       
       5)
       {{l|clear_list:''}} -> [1, '1', ' ', None, 'www.abc.com']
       
       6)
       {{l|clear_list:' '}} -> [1, '1', '', None, 'www.abc.com']
       
       7)
       {{l|clear_list:'www.abc.com'}} -> [ 1, '1', '', ' ', None]
       
       
       
       8)
       x = some_object(...)
       l = [1, 2, x]
       
       {{l|clear_list:x}} -> [1, 2]   
    '''
    
    try:
        return [i for i in list if i!=clear]
    except:
        return list

More like this

  1. clear cache view by zodman 1 year, 4 months ago
  2. Clear Django Cache by girasquid 5 years, 7 months ago
  3. clear session table by rubic 7 years, 1 month ago
  4. make templates fail loudly in dev by showell 4 years, 11 months ago
  5. Template tag to clear cached template fragment by joao.coelho 4 years, 5 months ago

Comments

darek (on January 7, 2011):

Better way to do it:

try:
    list.remove(clear)
except ValueError:
    pass
return list

PS. Don't use names for variables that are already in python builtins. list is python function:

>>> list
<type 'list'>
>>> list()
[]

Use for example data or even l instead.

#

(Forgotten your password?)