16 August 2010

Fourth part of AppEngine Forms. This time we delete added rows from database.

Basic Form Tutorial Google AppEngine
  1. AppEngine Forms in Python
  2. Submit AppEngine Forms Using POST in Python
  3. AppEngine Edit Form in Python
  4. Delete Row From Datastore in AppEngine

This is the last part of basic form handling in AppEngine. This time we make delete links.

Delete Class:

This class a link and makes a refresh/redirect.
Source code viewer
  1. class MyDeleteForm(webapp.RequestHandler):
  2. def post(self):
  3. id = int(self.request.get('id'))
  4. item = db.get(db.Key.from_path('MyModel', id))
  5. data = MyForm(data=self.request.POST, instance=item)
  6. if data.is_valid():
  7. entity = data.save(commit=False)
  8. entity.delete()
  9. self.redirect('/')
Programming Language: Python

Generate Delete Links:

This is how you can generate delete links. If you use views and django template engine. You have to use google app engines template library and pass on the array of data through template values.
Source code viewer
  1. #Import this library, goes in the beginning of the file as usual
  2. from google.appengine.ext.webapp import template
  3. #This is is the array of the data. Comes from datastore using GQL.
  4. db.GqlQuery("SELECT * FROM MyModel")
Programming Language: Python
This is the view file. If you don't know how to use views then here is a tutorial: Using Templates.
Source code viewer
  1. {% for row in query %}
  2. <a href="delete?id={{ row.key.id }}">Delete: {{ row.title }}</a><br />
  3. {% endfor %}
Programming Language: HTML
You can use the same way to make the edit links automatically.
This is the end of this tutorial series. Now you should have a basic form where you can submit, edit and delete data.