Python 在html模板中打印appengine模型实体id
以下是我拥有的简单数据库模型:Python 在html模板中打印appengine模型实体id,python,google-app-engine,jinja2,webapp2,Python,Google App Engine,Jinja2,Webapp2,以下是我拥有的简单数据库模型: class Notes(db.Model): text = db.StringProperty(multiline=True) date = db.DateTimeProperty(auto_now_add=True) 现在在url处理程序中,我将所有注释发送到模板,如下所示: class MainPage(webapp2.RequestHandler): def get(self): notes = Notes.all() self
class Notes(db.Model):
text = db.StringProperty(multiline=True)
date = db.DateTimeProperty(auto_now_add=True)
现在在url处理程序中,我将所有注释发送到模板,如下所示:
class MainPage(webapp2.RequestHandler):
def get(self):
notes = Notes.all()
self.render_template("index.html", {'notes':notes})
在使用模板引擎的模板中,我希望打印每个注释的id
,以便嵌入编辑链接,有点像这样:
{% for note in notes %}
<p>
{{ note.text }} <br>
{{ note.date }} <br>
(<a href ="edit/{{note.key.id}}">edit </a>)
{% endfor %}
{%用于notes%中的注释]
{{note.text}}
{{note.date}}
()
{%endfor%}
但问题是,我没有看到任何打印的内容,而不是note.key.id
根据docs overkey
类表示数据库实体的唯一键,它有一个方法id
,它是一个数值。对于笔记集合中的单个笔记,我需要笔记的id
如果我使用django模板引擎,我会打印值{{notes.key.id}
,但是使用jinja2
我看不到它
我该怎么做呢?我们将
href
行替换为以下内容:
(<a href ="edit/{{note.key().id()}}">edit </a>)
()
这就足够了。我刚刚测试了它,在我的情况下,我必须按照如下方式调整您的解决方案:
{{job.key.id()}
(key,而不是key()),使用python 2.7、flask、jinjia2、ndb。。。