need to get entity key to delete entity


I am trying to delete an entity from the Datastore using a link in html. I understand that in order to do this, I need to have the entity's key so that I know which entity to "pair" the delete link with, so to speak. I can't for the life of me figure out how to do this...

Here is my html file that shows all of the cars in the database:

{% if cars|length > 0 %} {% for c in cars %} <tr> <td>{{ c.make }}</td> <td>{{ c.model }}</td> <td>{{ c.year }}</td> <td> {% for i in c.color %} {{ i }} {% endfor %} </td> <td>{{ c.condition }}</td> <td>{{ c.date }}</td> <td> <a href="/view_cars/{{ c.key().id() }}">Delete Car</a> </td> </tr> {% endfor %} {% endif %}

Here is the python file:

class AddCarHandler(webapp2.RequestHandler): template_variables = {} def get(self): template = JINJA_ENVIRONMENT.get_template('index.html') self.response.write(template.render(self.template_variables)) action = self.request.get('action') #if the user adds a car if action == 'add_car': c = car_database.Car() c.make = self.request.get('car-make') c.model = self.request.get('car-model') c.year = self.request.get('car-year') c.color = self.request.get_all('car-color') c.condition = self.request.get('car-condition') c.date = self.request.get('car-date') car_key = c.put() class ViewCarHandler(webapp2.RequestHandler): template_variables = {} def get(self): car = car_database.Car() #ndb query self.template_variables['cars'] = [{'make':x.make, 'model':x.model, 'year':x.year, 'color':x.color, 'condition':x.condition, 'date':x.date} for x in car_database.Car.query().fetch()] template = JINJA_ENVIRONMENT.get_template('/view_cars.html') self.response.write(template.render(self.template_variables))


You can get the key of an entity (which, say, is obtained through a query) like this:

entity_key = entity.key

Note: this only works <em>after</em> the entity was saved into the DB, not before (i.e. entity.put() was called at least once).

To pass the key between the python code and URLs or HTML code, from <a href="https://cloud.google.com/appengine/docs/python/ndb/entities#retrieving_entities" rel="nofollow">the documentation</a>, you can use a key's url string or a pre-computed deletion url based on that string, passed, for example, in self.template_variables['cars']:


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

You'll need to add a handler for such deletion url, in which you'd reconstruct the key as quoted above, then call:



  • Python decimal.Decimal id not the same
  • Google App Engine Launcher is not running my hello world for Python Mac
  • Google App Engine add image to page with static folder
  • converting Twilio sms body to a string - encoding error for @ symbol
  • Flot returns incorrect x value (mm/dd/yy - date) after zoom for stack bar chart
  • 'Enemy' was not declared in this scope?
  • JDBI's @BindBean doesn't find named parameters in bean class during INSERT
  • Creating JS objects in PHP with commas in between
  • Deleting empty Series out of Graph (with VBA)
  • How do I Dispose a HttpResponseMessage in my Web Api Method?
  • Failed to find version-tag string. File must be updated
  • PayPal API Listener Website Payments Standard URI
  • redirect_to root_url and return unless @user.activated
  • import scipy.sparse failed
  • User messaging system
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • where do I find the xml.dom python package for the python-2.6.0-8.9.28 and I have a suse/x86_64 vers
  • Xstream to map “choice” elements of XML
  • Python pickle not one-to-one: different pickles give same object
  • Low TTL with Leveled Compaction, should I reduce gc_grace_seconds to improve read performance withou
  • presentShareDialogWithParams posts to FB wall, but callback handler results say error
  • Button click event not firing in jQuery
  • Creating Java object from class name with constructor, which contains parameters [duplicate]
  • Webgrid not refreshing after delete MVC
  • How to write order and limit within cakephp joins array
  • How do I pass the string value parameter of the selected list item from an auto-populated dropdown l
  • Jquery UI tool tip close icon
  • why overloaded new operator is calling constructor even I am using malloc inside overloading functio
  • Adding a button at the bottom of a table view
  • Getting last autonumber in access
  • HTML download movie download link
  • MySQL WHERE-condition in procedure ignored
  • Release, debug version and Authorization Google?
  • Delete MySQLi record without showing the id in the URL
  • GridView Sorting works once only
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • Hits per day in Google Big Query
  • unknown Exception android
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app