google app engine - need to get entity key to delete entity -


i trying delete entity datastore using link in html. understand in order this, need have entity's key know entity "pair" delete link with, speak. can't life of me figure out how this...

here html file shows of cars in database:

{% if cars|length > 0 %}         {% c in cars %}             <tr>                 <td>{{ c.make }}</td>                 <td>{{ c.model }}</td>                 <td>{{ c.year }}</td>                 <td>                     {% in c.color %}                         {{ }}                     {% 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 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 user adds 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} 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 key of entity (which, say, obtained through query) this:

entity_key = entity.key 

note: works after entity saved db, not before (i.e. entity.put() called @ least once).

to pass key between python code , urls or html code, the documentation, can use key's url string or pre-computed deletion url based on string, passed, example, in self.template_variables['cars']:

you can use entity's key obtain encoded string suitable embedding in url:

url_string = sandy_key.urlsafe() 

this produces result agvozwxsb3ipcxihqwnjb3vudbiziwim can later used reconstruct key , retrieve original entity:

sandy_key = ndb.key(urlsafe=url_string) sandy = sandy_key.get() 

you'll need add handler such deletion url, in you'd reconstruct key quoted above, call:

entity_key.delete() 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -