marcelo.delta
marcelo.delta

Reputation: 3072

Django template tags filter with multiple arguments

Django template tags filter with multiple arguments

@register.filter
def customTag(value, first, second):
...
return result

Template

{{ valor|customTag:first|customTag:second }}

Error

customTag requires 3 arguments, 2 provided

Upvotes: 2

Views: 6911

Answers (3)

Andre Arruda
Andre Arruda

Reputation: 59

I had this doubt too. I got a good result using {% with as %} in django template, but I needed to create two filters template tags:

Template tags:

@register.filter
def customTag(value, first):
...
return result1

@register.filter
def customTag2(first, second):
...
return result2

Template html:

{% with value|custom_tag:first as r1%}
    {% with r1|custom_tag2:second as r2 %}
        use your r2 value: {{ r2 }}
    {% endwith %}
{% endwith %}

Upvotes: 2

ruddra
ruddra

Reputation: 51948

You can't pass multiple arguments to a filter(reference). Instead, you can do it like this:

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

{{ valor|customTag:"first,second"}}  // pass comma separated arguments in string

Upvotes: 9

Shakil
Shakil

Reputation: 4630

I think just passing whole customTag instead of argument may solve the problem. Perhaps there are other possible solution possible.

@register.filter("filter_of_custom_tag")
def customTag(custom_tag_instance):
... 
return result

And in your template

{{ customTag|filter_of_custom_tag}}

Upvotes: 0

Related Questions