Jeremy
Jeremy

Reputation: 1845

Django TemplateSyntaxError Could not parse the remainder

Why can't I type this in a Django template?

data|customTag:variable,forloop.parentloop.counter

I just want to be able to pass three or more arguments into a filter

pretend there are already for loop and the variable/filter have been defined elsewhere

Upvotes: 2

Views: 3167

Answers (2)

Alasdair
Alasdair

Reputation: 309109

In your example, customTag is a filter, not a template tag.

According to the docs, Django template filters only take the input (in your case data), and one optional argument. You are getting the error because you are trying to pass more than one argument, which is not possible.

You could write a custom template tag instead. The syntax in your template would be:

{% customTag data variable forloop.parentloop.counter %} 

Upvotes: 1

alecxe
alecxe

Reputation: 474231

This is not possible since django template filters accept only one argument by definition:

Custom filters are just Python functions that take one or two arguments:

  • The value of the variable (input) – not necessarily a string.

  • The value of the argument – this can have a default value, or be left out altogether.

There is a workaround suggested here that might work for your use case.

Another possible solution would be to split your tag with 2 input arguments into two tags with a single one and chain them in the template. It depends on the logic you have in the filter, but can be an option.

Upvotes: 1

Related Questions