[英]How to drill down from entity list to entity instance in Google Appengine?
I have a list of entities, and want to use the entity key to link to more details of an individual entity. 我有一个实体列表,并且想要使用实体键链接到单个实体的更多详细信息。
class RouteDetails(ndb.Model):
"""Get list of routes from Datastore """
RouteName = ndb.StringProperty()
@classmethod
def query_routes(cls):
return cls.query().order(-cls.RouteName)
class RoutesPage(webapp2.RequestHandler):
def get(self):
adminLink = authenticate.get_adminlink()
authMessage = authenticate.get_authmessage()
self.output_routes(authMessage,adminLink)
def output_routes(self,authMessage,adminLink):
self.response.headers['Content-Type'] = 'text/html'
html = templates.base
html = html.replace('#title#', templates.routes_title)
html = html.replace('#authmessage#', authMessage)
html = html.replace('#adminlink#', adminLink)
html = html.replace('#content#', '')
self.response.out.write(html + '<ul>')
list_name = self.request.get('list_name')
#version_key = ndb.Key("List of routes", list_name or "*notitle*")
routes = RouteDetails.query_routes().fetch(20)
for route in routes:
routeLink = '<a href="route_instance?key={}">{}</a>'.format(
route.Key, route.RouteName)
self.response.out.write('<li>' + routeLink + '</li>')
self.response.out.write('</ul>' + templates.footer)
The error I am getting is AttributeError: 'RouteDetails' object has no attribute 'Key'
. 我得到的错误是
AttributeError: 'RouteDetails' object has no attribute 'Key'
。
How do I reference the unique ID of the entity in my drilldown URL? 如何在下钻 URL中引用实体的唯一ID?
The RouteDetails
object indeed has not Key
attribute, so you will get an exception at route.Key
. RouteDetails
对象确实没有Key
属性,因此您将在route.Key
处获得异常。
To get an entity's key you need to invoke the key
attribute/property: route.key
. 要获取实体的密钥,您需要调用
key
属性/属性: route.key
。
But passing the entity's key directly through HTML does not work since it's an object. 但是,由于实体键是对象,因此无法直接通过HTML传递实体键。 The
urlsafe()
method is available to provide a string representation of the key object that is OK to use in HTML. urlsafe()
方法可用于提供可以在HTML中使用的键对象的字符串表示形式。
So do something along these lines instead: 因此,请按照以下方式进行操作:
for route in routes:
routeLink = '<a href="route_instance?key={}">{}</a>'.format(
route.key.urlsafe(), route.RouteName)
self.response.out.write('<li>' + routeLink + '</li>')
See also Linking to entity from list 另请参阅从列表链接到实体
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.