- Author:
- Tomek
- Posted:
- December 23, 2010
- Language:
- Python
- Version:
- Not specified
- Score:
- 0 (after 0 ratings)
Clear list from unwanted elements, within django template.
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
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Better way to do it:
PS. Don't use names for variables that are already in python builtins.
list
is python function:Use for example
data
or evenl
instead.#
Please login first before commenting.