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

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

问题描述

我有一个咨询页面,列出数据存储中的咨询。列表循环如下所示:

  {%咨询咨询%} 
< 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( ** consulting_dic))

我想知道我在列表中查询的基本概念一个链接进入并查看有关该特定咨询的信息。



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



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

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

到我的ConsultsPage(咨询列表)。处理程序现在看起来像这样:

$ p $ 类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'对象没有属性'key'

在我的循环中列出咨询的链接href中,我该怎么做?是这样的:

  href =咨询/ {{url}}


解决方案

从键中检索实体


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

  url_string = sandy_key。 urlsafe()

结果如 agVoZWxsb3IPCxIHQWNjb3VudBiZiwIM
可以用来重建密钥并检索原始的
实体:

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


因此,对于每个请参阅实体,您可以获得一个唯一的URL来显示有关该实体的信息。例如通过使用URL参数:

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

并且在 / display_consult 页面处理程序中你会得到这样的实体:

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


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%}

The handler is like this:

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.

Edit I have added the line:

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))

However I get this error:

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

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

href="consults/{{ url }}"

解决方案

From Retrieving Entities from Keys:

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

url_string = sandy_key.urlsafe()

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()

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()

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天全站免登陆