views:

347

answers:

1

I know there is a post here: http://stackoverflow.com/questions/1333189/django-template-system-calling-a-function-inside-a-model describing how you can make a custom template filter to achieve this, but from a programmer's standpoint this is a failure because that's hacking something that isn't meant for that. It seems almost ridiculous that you can't call a method with parameters in Django's template system. Any help is much appreciated.

Thanks, Orokusaki

+3  A: 

The Django team has decided, as a matter of philosophy, not to allow passing method parameters in a view. Personally, I agree with them; it forces a separation of logic and presentation that I find helpful. It prevents the kind of spaghetti code that PHP is notorious for.

The right thing to do in the case you linked is to pass the result of that call from the view into the template via the context. It's just more maintainable that way. If later you need to change my_related_deltas(3) to my_related_deltas(4), you go to the view, which should be fairly concise, instead of searching through templates to figure out exactly where it is defined.

tghw
Thanks for your answer. I strongly disagree with their philosophy, but I don't have time to build a full stack framework, so I'll live with it. The guys over at Django HQ should take a look at the Velocity project if they want to see what is the best template system in the world. That's not subjective. It's a fact. I'll have to pickup Cheetah and attach that instead.
orokusaki
Well, the nice thing is that they don't tie you to it. I know a few people who use Jinja2 (http://jinja.pocoo.org/2/documentation) for their templates instead of the Django template system. Apparently it's pretty easy to switch out and use instead.
tghw
Guide to replacing the Django template system here: http://lethain.com/entry/2008/jul/22/replacing-django-s-template-language-with-jinja2/
tghw