django模板调用函数 [英] django template call function
问题描述
每当我在模型内部拥有一个db.ReferenceProperty()时:
class User(db.Model):
name = db.StringProperty()
avatar = db.ReferenceProperty(dbImage)
因此,在放出用户页面时,在django模板中,我无法做到这一点
< div>
< span> {{user.name}}< / span>
< span> {{user.avatar.key()}}< / span>
< / div>
我现在正在做的是在数据出来之前写一点python代码模板,看起来像
user.avatarsKey = user.avatar.key()
然后
< div>
< span> {{user.name}}< / span>
< span> {{user.avatarsKey}}< / span>
< / div>
消除函数调用。我不喜欢这个,因为我必须在很多地方完成它,并开始变得混乱。有没有办法从模板中调用db对象的.key()方法?
函数调用只是函数获取。在你的例子中,试试:
{{user.avatar.key}}
我知道,这很奇怪。但是,嘿,数组/列表更糟糕:
{{user.mylist.0}}
code>
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?
解决方案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 }}
这篇关于django模板调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!