views:

190

answers:

1

Using google appengine and django.

Whenever I have a db.ReferenceProperty() inside a model like:

class User( db.Model ) :
    name = db.StringProperty()
    avatar = db.ReferenceProperty( dbImage )

So when putting out a User's page, in the django template I CAN'T do

<div>
    <span>{{ user.name }}</span>
    <span>{{ user.avatar.key() }}</span>
</div>

What I'm doing right now is writing a bit of python code before the data goes out to the template that looks like

user.avatarsKey = user.avatar.key()

Then

<div>
    <span>{{ user.name }}</span>
    <span>{{ user.avatarsKey }}</span>
</div>

eliminating the function call. I don't like this though, because I have to do it in a lot of places and its starting to get cluttery. Is there a way to invoke the .key() method of a db object from inside the template?

+5  A: 

In django templates, function invocation is just function getting. in your example, try:

{{ user.avatar.key }}

I know, it's weird. But hey, it's even worse with arrays/lists:

{{ user.mylist.0 }}
pavpanchekha
+1 for correct. the list thing threw me off too.
George
-------ty-------
bobobobo
Remarkably this works even when the `avatar` object is None. It doesn't throw a NoneType error or anything like that. Amazing.
bobobobo
Yeah, django templates don't throw errors. That bothers me, so I normally use genshi for any reasonably large project. The philosophy is, you don't want a damn know-nothing designer to crash your sight. Agree or disagree as you may.
pavpanchekha
So, you can't send arguments to a function from a django template,
bobobobo
Not as far as I know. Again, I dislike the django template engine, and it's not hard at all to replace it with something sane, like Genshi.
pavpanchekha