Question

I am aware that one can create template filters in Django templates such as

{{ "abcdef"|makeUppercase }}

Can one create template filters that accept two arguments in Django? Suppose I wanted to create a template filter that found the logarithm of n to base m. How do I do that?

Thank you.

Was it helpful?

Solution

That's not a template tag, that's a template filter. And yes, you can accept an additional argument:

{{ foo|my_filter:"bar" }}

The arguments to this filter are the value of foo and "bar". Note that you can only send two arguments: if you need more, you'll have to pass them inside the second argument separated by commas, and parse them within the template.

If you just need a standalone template tag, you can use the @simpletag decorator to write one that takes as many arguments as you like. These are called by the normal tag syntax:

{% my_tag foo bar baz %}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top