Linking to entity from list

前端 未结 1 574
耶瑟儿~
耶瑟儿~ 2021-01-24 09:35

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

{% for consult in consults %}
 
  

        
相关标签:
1条回答
  • 2021-01-24 09:48

    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()
    
    0 讨论(0)
提交回复
热议问题