Last active
October 31, 2019 08:39
-
-
Save fdobrovolny/e9552294b3059461f940a47143f58811 to your computer and use it in GitHub Desktop.
Pass multiple parametrs into Django filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Pass multiple parametrs into Django filter. | |
Inspired by http://stackoverflow.com/a/24402622/2629036. | |
By this filters you can chain as many variables into parametrs as you like. | |
You can even make different lengths for one filter. | |
WARNING: You have to apply n_chain_end filter at the end of the n_chains. | |
Thanks to this the intial var can also be tupple. | |
Usage of n_chain: | |
{{ var|n_chain_more:1|n_chain_more:2|n_chain_more:3|n_chain_end|your_filter }} | |
The output from this will look like: | |
(var, 1, 2, 3) | |
NOTE: if var was tupple (elements_of_my_tupple, None, 5, 10) the resoult would look like: | |
((elements_of_my_tupple, None, 5, 10), 1, 2, 3) | |
Usage of n_more: | |
{{ var|n_more:1|n_more:2|n_more:3|your_filter }} | |
The output from this will look like: | |
(var, 1, 2, 3) | |
NOTE: if var was tupple (elements_of_my_tupple, None, 5, 10) the resoult would look like: | |
(elements_of_my_tupple, None, 5, 10, 1, 2, 3) | |
""" | |
from django import templateer.py | |
register = template.Library() | |
class Chain(object): | |
pass | |
@register.filter | |
def n_chain_more(_n, _2): | |
"""Chain element.""" | |
if type(_n) == tuple and _n[-1] == Chain: | |
return _n[:-1] + (_2, Chain) | |
return _n, _2, Chain | |
@register.filter | |
def n_chain_end(_n): | |
"""Chain terminator.""" | |
if type(_n) == tuple and _n[-1] == Chain: | |
return _n[:-1] | |
return _n | |
@register.filter | |
def n_more(_n, _2): | |
"""Append element to tupple.""" | |
if type(_n) == tuple: | |
return _n + (_2,) | |
return (_n, _2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment