0
我想從數據存儲中使用html中的鏈接刪除實體。我知道爲了做到這一點,我需要擁有實體的密鑰,以便我知道哪個實體將刪除鏈接「配對」,可以這麼說。我不能爲我的生活弄清楚如何做到這一點...需要獲取實體鍵來刪除實體
這裏是我的html文件,顯示數據庫中的所有汽車的:
{% 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 %}
這裏是Python文件:
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))