16 August 2010

Third part of AppEngine Forms. This time we are making edit from.

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 time we will make an edit form. This is different because fields have to be populated before and saved/updated after. This is just a form, you have to make your views or html lists some other way to make use of the edit buttons.

Edit Form:

Lets make another class. I call it MyEditForm. This is a bit different, fields are pre populated and instead of submit we use save.
Source code viewer
  1. class MyEditForm(webapp.RequestHandler):
  2. def get(self):
  3. self.form()
  4. def post(self):
  5. id = int(self.request.get('_id'))
  6. item = db.get(db.Key.from_path('MyModel', id))
  7. data = MenuForm(data=self.request.POST, instance=item)
  8. if data.is_valid():
  9. entity = data.save(commit=False)
  10. entity.put()
  11. self.redirect('/')
  12. else:
  13. self.form()
  14. def form(self):
  15. self.response.out.write('<html><body>'
  16. '<form method="POST" action="/edit">'
  17. '<table>')
  18. self.response.out.write(MyForm(instance=item))
  19. self.response.out.write('</table>'
  20. '<input type="submit">'
  21. '</form></body></html>')
Programming Language: Python
The url has to contain the id variable. That way you can make the edit button or link. For example:
  1. <a href="/edit?id=1">Edit</a>
You can add this class to our previously made code. You also have to add the class in the bottom of the page where are the url routes.