python - 在 html 模板中打印 appengine 模型实体 ID

标签 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.render_template("index.html", {'notes':notes})

在我使用的模板中 jinja2模板引擎,我想要打印每个笔记的 id,这样我就可以嵌入一个编辑链接,有点像这样:

{% for note in notes %}
<p>  
 {{ note.text }} <br>
 {{ note.date }} <br>
 (<a href ="edit/{{note.key.id}}">edit </a>)
{% endfor %}

但问题是,我没有看到任何打印的内容,而是 note.key.id 根据 here 上的文档key 类表示数据库实体的唯一键,它有一个方法 id,它是一个数值。对于笔记集合中的单个笔记,我想要笔记的 id

如果我使用 django 模板引擎,我会打印出值 {{ notes.key.id }},但是使用 jinja2 我看不到它。

我该怎么做?

最佳答案

我们将 href 行替换为以下内容:

(<a href ="edit/{{note.key().id()}}">edit </a>)

这就够了。

关于python - 在 html 模板中打印 appengine 模型实体 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10340439/

相关文章:

python - 从半径为 R1 和 R2 的圆环内的图像中提取数据点

python - 根据第一个字符的出现分隔字符串列

google-app-engine - 文件不可访问 : 'templates/test.html' using jinja2 templates in certain directories

python - 为什么可以在ansible中使用分割过滤器?

python - 为 jinja2 中的未定义属性引发异常

python - 无法运行 Whoosh 'quickstart' 示例 (FileNotFoundError)

python - `ipython` 选项卡自动完成对导入的模块不起作用

python - 我如何使用带有 python 的 cron 来使用谷歌驱动器?

python - 强制YouTube API upload_video.py身份验证在外部浏览器中打开?

android - 如何从 Google App Engine 应用程序发送 Firebase 云消息