Reputation: 4329
I want to delete the selected value from the database in ndb, I have the following html code:
Getting the Data in Rooms as:
Class Method:
all_rooms = Room.query().fetch()
template_values = {
'logout_url': users.create_logout_url(self.request.uri),
'rooms': all_rooms
}
template = JINJA_ENVIRONMENT.get_template('rooms.html')
self.response.write(template.render(template_values))
HTML:
{% for i in rooms %}
<br/>
<form action="/rooms" method="post">
<input type="hidden" name="roomKey" value="{{ i.key }}"/>
<input type="submit" value="Delete" name="button"/>
</form>
{{i.key}}
Room Nummber:{{ i.roomNumber }}
<br/>
{% endfor %}
Which OutPuts As Following:
Key('Room', 4644337115725824) Room Nummber:10
I am passing the key to the delete method which is coming as: Key('Room', 4644337115725824)
How should I retrieve key from it in order to delete the value Room Value.
Upvotes: 0
Views: 56
Reputation: 3300
Use i.key.urlsafe()
. This will convert the key to a "url safe" string which is exactly where you plan to use it: in a url.
When you get the roomKey
values back, you can convert them back from strings in urlsafe format to ndb Key objects by doing ndb.Key(urlsafe=roomKey)
(see this link).
BONUS TIP: This would be extremely insecure code. Okay if this app is just for you (after sign-in) but dangerous if for untrusted users. Why? Well, someone could mock your HTML with keys for any object of any type and then simulate clicking the delete button. Your GAE-side code would blindly convert the urlsafe strings to keys and delete stuff.
Upvotes: 2