Multiple arguments in Django template filters
2012-04-26 11:16
519 查看
Multiple arguments in Django template filters | nme.pl/en
By default, it is not currently possible to pass multiple arguments to Django template filter — documentation states: "Custom filters are just Python functions that take one or two arguments".
Here I describe solution that allows passing more arguments.
Lets say we have
Multiple arguments in Django template filters
Author: nme · Tuesday, 5 July, 2011 · No comments ·By default, it is not currently possible to pass multiple arguments to Django template filter — documentation states: "Custom filters are just Python functions that take one or two arguments".
Here I describe solution that allows passing more arguments.
Lets say we have
keyand current
query_stringin our template context. We loop in paginator and alter query_string current key value with
page. The key is to group arguments in an array. We use following custom filter:
@register.filter def keys (first,second): if isinstance(first,list): return first+[second] else: return [first,second]following filter allows us to:
{{ "1"|keys:"2"|keys:"3" }}which will return in our template:
[u'1', u'2', u'3']Returning to described query_string altering problem — we use
alter_queryfilter:
@register.filter def alter_query (keys, query_string): from django.http import QueryDict query_dict = QueryDict(query_string, mutable=True) query_dict[keys[0]] = keys[1] return query_dict.urlencode()inside pagination template, we use following code:
<a href="?{{ key|keys:page|alter_query:query_string }}">{{ page }}</a>Doesn't look pretty, but works perfectly.
相关文章推荐
- How do I add multiple arguments to my custom template filter in a django template? - Stack Overflow
- WARNINGS: ?: (1_8.W001) The standalone(独立) TEMPLATE_* settings were deprecated(弃用) in Django 1.8 and
- django notes 4 template tags and filters
- Built-in template tags and filters
- Django:ExtendsNode: extends 'base/base.html'> must be the first tag in the template.
- :(23, 7) in class Queen, multiple overloaded alternatives of constructor Queen define default arguments. class Queen private(val name:String,prop:Array[String],private[scala02] val age:Int = 18){
- 在django模板中定义变量(set variable in django template)
- django1.8 提示(1_8.W001) The standalone TEMPLATE_* settings were deprecated in Django 1.8 and the TEMPLATES dictionary takes precedence. You must put the values of the following settings into your defau
- he method get(Class, Serializable) in the type HibernateTemplate is not applicable for the arguments
- [Django](1093, "You can't specify target table 'fee_details_invoices' for update in FROM clause") 错误
- Teddy's Aspect Weaver Version 0.3 with Great Updating and Fixing, Especially the Implementing of Getting Runtime Method Context Info and Method Arguments in MSIL Level
- [golang] call the struct function in the template range
- use CheckBoxMultipleChoice in wicket
- Docker distrubution in django
- Django学习笔记(二)Django使用template
- django下不通过view直接从url跳转到template
- Events and Event Filters in Qt
- Django 1.6: 在template里访问session
- [django]添加自定义template filter标签
- Optional Parameters and Named Arguments in C# 4