Calling model-functions in template

7,870 views
Skip to first unread message

Kai Kuehne

unread,
Dec 2, 2006, 5:16:10 PM12/2/06
to django...@googlegroups.com
Hello list!
Is it possible to call functions of the model out of the template?
Sorry, didn't find it in the docs.

Example:
{{ model.function() }}

Thanks
Kai

James Bennett

unread,
Dec 2, 2006, 5:49:08 PM12/2/06
to django...@googlegroups.com
On 12/2/06, Kai Kuehne <kai.k...@gmail.com> wrote:
> Is it possible to call functions of the model out of the template?
> Sorry, didn't find it in the docs.

It is possible to call methods which take no arguments, by doing

{{ model.method }}

Note that this has 'method', not 'method()' -- parentheses are not
used, and it is not possible to pass arguments to the method. This is
done because the template language is not meant to be a full
programming language; if you need to call something with arguments,
you will need to do it in your view and pass the result data to the
template, or write a custom template tag which returns the data you
want.

--
"May the forces of evil become confused on the way to your house."
-- George Carlin

Kai Kuehne

unread,
Dec 2, 2006, 7:04:44 PM12/2/06
to django...@googlegroups.com
Hi,

On 12/2/06, James Bennett <ubern...@gmail.com> wrote:
> Note that this has 'method', not 'method()' -- parentheses are not
> used, and it is not possible to pass arguments to the method. This is
> done because the template language is not meant to be a full
> programming language; if you need to call something with arguments,
> you will need to do it in your view and pass the result data to the
> template, or write a custom template tag which returns the data you
> want.

Okay, thank you James. :)
Kai

Kai Kuehne

unread,
Dec 2, 2006, 7:09:49 PM12/2/06
to django...@googlegroups.com
I must misunderstand something really basic here. Just look:

Template:
<td>{{ movie.genres_title_list }}</td>

models.py:
def genres_title_list(self, separator=', '):
return separator.join([x.title for x in self.genres])

Prints nothing... idea anyone?
Kai

Todd O'Bryan

unread,
Dec 2, 2006, 7:32:34 PM12/2/06
to django...@googlegroups.com

Which class is that in? Movie?

Are you sure that the movie you're using has genre, i.e., have you tried
it in the shell?

Todd

Ivan Sagalaev

unread,
Dec 3, 2006, 5:12:05 AM12/3/06
to django...@googlegroups.com
Kai Kuehne wrote:
> models.py:
> def genres_title_list(self, separator=', '):
> return separator.join([x.title for x in self.genres])

If I reconstruct the case correctly and `genres` is a relation to child
objects with ForeignKey to a Movie then `self.genres` by default should
look like `self.genre_set.all()`. Otherwise it will raise an exception
(because there is no `self.genres`) and the template system won't show
anything there.

Kai Kuehne

unread,
Dec 3, 2006, 6:43:42 AM12/3/06
to django...@googlegroups.com
Hi,

On 12/3/06, Ivan Sagalaev <Man...@softwaremaniacs.org> wrote:
> Kai Kuehne wrote:
> [model.py]


>
> If I reconstruct the case correctly and `genres` is a relation to child
> objects with ForeignKey to a Movie then `self.genres` by default should
> look like `self.genre_set.all()`. Otherwise it will raise an exception
> (because there is no `self.genres`) and the template system won't show
> anything there.

No no, this is ok. I have tested it in the shell.

Greetings
Kai

Reply all
Reply to author
Forward
0 new messages