如何在 django 模板中向我的自定义模板过滤器添加多个参数? [英] How do I add multiple arguments to my custom template filter in a django template?

查看:42
本文介绍了如何在 django 模板中向我的自定义模板过滤器添加多个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

from django import template

register = template.Library()

@register.filter
def replace(value, cherche, remplacement):
    return value.replace(cherche, remplacement)

以下是我在模板文件中尝试使用它导致错误的方法:

{{ attr.name|replace:"_"," " }}
{{ attr.name|replace:"_" " " }}
{{ attr.name|replace:"_":" " }}
{{ attr.name|replace:"cherche='_', remplacement=' '" }}

我查看了 django 的文档book 但只找到了使用单个参数的例子......它甚至可能吗?

I looked into django's docs and book but only found example using a single argument... is it even possible?

推荐答案

这是可能的,而且相当简单.

It is possible and fairly simple.

Django 只允许您的过滤器使用一个参数,但没有理由不能将所有参数放入一个字符串中,并使用逗号分隔它们.

Django only allows one argument to your filter, but there's no reason you can't put all your arguments into a single string using a comma to separate them.

例如,如果您想要一个过滤器来检查变量 X 是否在列表 [1,2,3,4] 中,您将需要一个如下所示的模板过滤器:

So for example, if you want a filter that checks if variable X is in the list [1,2,3,4] you will want a template filter that looks like this:

{% if X|is_in:"1,2,3,4" %}

现在我们可以像这样创建模板标签:

Now we can create your templatetag like this:

from django.template import Library

register = Library()

def is_in(var, args):
    if args is None:
        return False
    arg_list = [arg.strip() for arg in args.split(',')]
    return var in arg_list

register.filter(is_in)

创建 arg_list 的行是一个生成器表达式,它在所有逗号上拆分 args 字符串并调用 .strip() 以删除任何前导和尾随空格.

The line that creates arg_list is a generator expression that splits the args string on all the commas and calls .strip() to remove any leading and trailing spaces.

例如,如果第 3 个参数是一个 int,那么就这样做:

If, for example, the 3rd argument is an int then just do:

arg_list[2] = int(arg_list[2])

或者如果它们都是整数,则:

Or if all of them are ints do:

arg_list = [int(arg) for arg in args.split(',')]

现在要通过使用键值对作为参数来专门回答您的问题,您可以使用 Django 用于从 URL 中解析查询字符串的相同类,这也具有根据您的要求正确处理字符编码的好处设置.py.

now to specifically answer your question by using key,value pairs as parameters, you can use the same class Django uses to parse query strings out of URL's, which then also has the benefit of handling character encoding properly according to your settings.py.

因此,与查询字符串一样,每个参数都由&"分隔:

So, as with query strings, each parameter is separated by '&':

{{ attr.name|replace:"cherche=_&remplacement= " }}

那么你的替换函数现在看起来像这样:

Then your replace function will now look like this:

from django import template
from django.http import QueryDict

register = template.Library()

@register.filter
def replace(value, args):
    qs = QueryDict(args)
    if qs.has_key('cherche') and qs.has_key('remplacement'):
        return value.replace(qs['cherche'], qs['remplacement'])
    else:
        return value

您可以加快一些速度,但可能会进行一些不正确的替换:

You could speed this up some at the risk of doing some incorrect replacements:

qs = QueryDict(args)
return value.replace(qs.get('cherche',''), qs.get('remplacement',''))

这篇关于如何在 django 模板中向我的自定义模板过滤器添加多个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆