Abi Waqas
Abi Waqas

Reputation: 396

How to pass three arguments to django templatetags filter?

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

My template tags-

@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 }}

and the error it shows: status_of_booking requires 3 arguments, 2 provided

Upvotes: 0

Views: 2187

Answers (1)

Alasdair
Alasdair

Reputation: 308779

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

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'

Then in your template:

{{ item|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'

and change your template to:

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

Upvotes: 3

Related Questions