带有多个参数的 Django 模板标签过滤器

mar*_*lta 1 python django django-templates python-2.7 python-3.x

带有多个参数的 Django 模板标签过滤器

@register.filter
def customTag(value, first, second):
...
return result
Run Code Online (Sandbox Code Playgroud)

模板

{{ valor|customTag:first|customTag:second }}
Run Code Online (Sandbox Code Playgroud)

错误

customTag 需要 3 个参数,提供 2 个

rud*_*dra 5

您不能将多个参数传递给过滤器(参考)。相反,您可以这样做:

@register.filter
def customTag(value, args):
   first, second = args.split(',')
   ...
   return value

{{ valor|customTag:"first,second"}}  // pass comma separated arguments in string
Run Code Online (Sandbox Code Playgroud)