【问题标题】:Retrieve ID from entity in datastore从数据存储区中的实体检索 ID
【发布时间】:2017-12-20 17:51:45
【问题描述】:

我正在尝试将查询结果输出到 HTML 表中,并在其旁边放置一个删除实体的链接。

如何从数据存储中检索每个实体的 ID,以便删除链接知道它需要删除哪个实体?我正在使用 Python/Webapp2/Jinja2。

HTML:

<table>
  <tr>
    <th><b>{{ result.email }}</th>
    <th><b>{{ result.date }}</th>
    <th><b>{{ result.title }}</th>
    <th><b>{{ result.content }}</th>
    <th><a href="/delete/{{ ID GOES HERE }}"</th> 
  </tr>
</table>

Python:

class MyRequestsHandler(webapp2.RequestHandler): # Queries the datastore

def get(self):
    user = users.get_current_user()
    userIdentity = users.get_current_user().user_id()
    #email = users.get_current_user().email()
    login_url = users.create_login_url(self.request.path)
    logout_url = users.create_logout_url(self.request.path)

    q = WorkRequest.query(WorkRequest.userId == userIdentity)

    results = q.fetch(10)

    template = template_env.get_template('myrequests.html')
    context = {

        'user': user,
        'login_url': login_url,
        'logout_url': logout_url,
        'results': results,
    }

    self.response.out.write(template.render(context))

【问题讨论】:

  • 为什么不向模板发送userIdentity
  • @GAEfan 我还在这个阶段做实验。不确定我是否需要它。

标签: python google-app-engine google-cloud-datastore jinja2


【解决方案1】:

您使用的是自定义 ID,还是来自 ndb 的标准整数 ID?我想你想要的是:

{{ result.key.id() }}

取决于您使用的型号。但您可以考虑向模板发送 url safe key 并使用它。

【讨论】:

  • pk 是错字吗?
  • 是的,django 的东西。固定。
猜你喜欢
  • 2014-12-30
  • 1970-01-01
  • 2020-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多