Trying to be loyal to django's template philosophy

May 24, 2012

I’ve reached the following dilemma.

“Model instance with methods that depends on logged user” VS “Django dislike to call a method with parameters in it’s template framework”

I agree with them (while wondering who cares)

So, what to do?

  • Messing with the view (as suggested). The problem is that sometimes I have to access the elements of a query and that would mean iterating through all the elements to update it’s status.
  • Messing with the model. Add a ‘user’ variable to each model. (prediction: same problem from above)
  • Messing with the template. This is the best solution I’ve have found but it’s not django’s way.

I’ve found MY solution, although I’m not so sure about its elegance.

Usage:

{{ instance|with_user:user|call_method:"my_method_name" }}

comments powered by Disqus

Of course it is happening inside your head ... Click!