How to call function that takes an argument in a Django template?


I'm passing to Django's template a function, which returns some records. I want to call this function and iterate over its result.

{% for item in my_func(10) %} 

That doesn't work. I've tried to set the function's return value to a variable and iterate over the variable, but there seems to be no way to set a variable in a Django template.

Is there any normal way to do it?

5/11/2016 10:15:26 PM

Accepted Answer

You cannot call a function that requires arguments in a template. Write a template tag or filter instead.

3/18/2010 9:58:02 AM

if you have an object you can define it as @property so you can get results without a call, e.g.

class Item:
    def results(self):
        return something

then in the template:

<% for result in item.results %>
<% endfor %>

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow