链接到列表中的实体 [英] Linking to entity from list

查看:18
本文介绍了链接到列表中的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个咨询页面,其中列出了数据存储中的咨询.列表循环是这样的:

I have a Consults page that lists consults in the datastore. The list loop is like this:

{% for consult in consults %}
 <tr>
  <td><a href="consults/#">{{ consult.consult_date }}</a></td>
  <td>{{ consult.consult_time }}</td>
  <td>{{ consult.patient_first }}</td>
  <td>{{ consult.patient_last }}</td>
  <td><span class="badge badge-warning">{{ consult.consult_status }}</span></td>
 </tr>
{%endfor%}

处理程序是这样的:

class ConsultsPage(webapp2.RequestHandler):
    def get(self):
        consults = Consults.query().fetch(5)
        consults_dic = {"consults" : consults}
        template = JINJA_ENVIRONMENT.get_template('/templates/consults.html')
        self.response.out.write(template.render(**consults_dic))

我想知道我如何使列表中的每个咨询成为进入并查看有关该特定咨询的信息的链接背后的基本概念.

I want to know the basic concept behind how I make each consult in the list a link to go in and view information about that particular consult.

我知道我需要使用密钥来检索实体,但我不确定该过程的其余部分.

I understand I need to use a key to retrieve an entity but am unsure of the rest of the process.

编辑我已经添加了这一行:

url = '/display_consult?key=%s' % consults.key.urlsafe()

到我的咨询页面(其中列出了咨询).处理程序现在看起来像这样:

to my ConsultsPage (where the consults are listed). The handler now looks like this:

class ConsultsPage(webapp2.RequestHandler):
    def get(self):
        consults = Consults.query().fetch(5)
        consults_dic = {"consults" : consults}
        url = '/display_consult?key=%s' % consults.key.urlsafe()
        template = JINJA_ENVIRONMENT.get_template('/templates/consults.html')
        self.response.out.write(template.render(**consults_dic))

但是我收到此错误:

url = '/display_consult?key=%s' % consults.key.urlsafe()
AttributeError: 'list' object has no attribute 'key'

另外,我在我的循环中列出咨询的链接 href 中放了什么?是不是像:

Also what do I put into the link href in my loop that lists consults? is it something like:

href="consults/{{ url }}"

推荐答案

来自 从键中检索实体:

您还可以使用实体的键来获取适合的编码字符串用于嵌入 URL:

You can also use an entity's key to obtain an encoded string suitable for embedding in a URL:

url_string = sandy_key.urlsafe()

这会产生类似于 agVoZWxsb3IPCxIHQWNjb3VudBiZiwIM 的结果,其中稍后可用于重建密钥并检索原始密钥实体:

This produces a result like agVoZWxsb3IPCxIHQWNjb3VudBiZiwIM which can later be used to reconstruct the key and retrieve the original entity:

sandy_key = ndb.Key(urlsafe=url_string)
sandy = sandy_key.get()

因此,对于每个 consult 实体,您可以获得一个唯一的 URL,您可以在其中显示有关该实体的信息.例如通过使用 URL 参数:

So for each consult entity you can obtain a unique URL where you'd display the info about that entity. For example by using a URL parameter:

url = '/display_consult?key=%s' % consult.key.urlsafe()

/display_consult 页面处理程序中,您将获得如下实体:

And in the /display_consult page handler you'd obtain the entity like this:

consult = ndb.Key(urlsafe=request.get('key')).get()

这篇关于链接到列表中的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆