我有一个实体列表,希望使用实体键链接到单个实体的更多详细信息
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)
我得到的错误是AttributeError: 'RouteDetails' object has no attribute 'Key'
如何在我的drilldownURL中引用实体的唯一ID
RouteDetails
对象确实没有Key
属性,因此您将在route.Key
处得到一个异常要获取实体的密钥,需要调用
key
属性/属性:route.key
但是通过HTML直接传递实体的键是不起作用的,因为它是一个对象。
urlsafe()
方法可用于提供可以在HTML中使用的键对象的字符串表示因此,请按照以下思路来做:
另见Linking to entity from list
相关问题 更多 >
编程相关推荐