Django – Simple Template Tag Without a String as Argument

I’ve never been too much into template tags, since as far as I could understand from the django docs, was only possible to get a simple template tag if passing a string as parameter.
But now I see that’s not true.

Simple Tag

If you take a look at the docs you’ll find an example like this:

That’s something nice, you can also change it a little, to just add something to the context:

Different Argument. Is it Possible?

You see… at first I thought that any method decorated by “@register.simple_tag” needed to have a string as argument.
But no, I just found out that you can use any parameters there, for example:

Let’s say you want to use in the templates a method from a Poll’s model that requires a user as argument.
You can do it like this:

Then in your template you would do as follows:

That’s it, hope this will help out anyone that was in doubt about if it’s possible to make a simple_tag without a string as argument =)

About arruda

Adoro programar, descobrir novas frameworks ágeis e suas diversas aplicações.