Django template call function
Using Google App Engine 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, becau开发者_JS百科se I have to do it in a lot of places and it's starting to get cluttery. Is there a way to invoke the .key()
method of a DB object from inside the template?
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 }}
精彩评论