如何将三个参数传递给Django templatetags过滤器? [英] How to pass three arguments to django templatetags filter?

查看:63
本文介绍了如何将三个参数传递给Django templatetags过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在Django的模板标签过滤器中传递两个参数,但有点困惑如何使用3个参数.

I know how to pass two parameters in template tag filter in Django but bit confused how to use 3 parameters.

我的模板标签-

@register.filter("status_of_booking")
def status_of_booking(status,create_date,from_time):
    print create_date
    print status
    return 'hola'

我正在传递三个这样的参数,并且显示错误:-

and i am passing three arguments like this and it is showing error:-

{{ item.status|status_of_booking:item.classSession.date,item.id }}

及其显示的错误: status_of_booking需要3个参数,提供2个参数

推荐答案

Django模板过滤器接受零或一个参数.不可能有多个参数.

A Django template filter takes either zero or one arguments. Multiple arguments are not possible.

在您的情况下,您可以使用 item 作为第一个参数,而不是 status :

In your case, you could use item as the first argument, instead of status:

@register.filter("status_of_booking")
def status_of_booking(item):
    print item.create_date
    print item.status
    return 'hola'

然后在您的模板中:

{{ item|status_of_booking }}

如果您确实需要多个参数,则必须将 status_of_booking 从过滤器更改为模板标记,

If you really need multiple arguments, then you would have to change status_of_booking from a filter to a template tag,

@register.simple_tag
def status_of_booking(status, create_date, from_time):
    print create_date
    print status
    return 'hola'

并将您的模板更改为:

{% status_of_booking item.status item.classSession.date item.id %}

这篇关于如何将三个参数传递给Django templatetags过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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