Third part of AppEngine Forms. This time we are making edit from.
Basic Form Tutorial Google AppEngine
- AppEngine Forms in Python
- Submit AppEngine Forms Using POST in Python
- AppEngine Edit Form in Python
- 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.The url has to contain the id variable. That way you can make the edit button or link. For example:Source code viewer
class MyEditForm(webapp.RequestHandler): def get(self): self.form() def post(self): id = int(self.request.get('_id')) item = db.get(db.Key.from_path('MyModel', id)) data = MenuForm(data=self.request.POST, instance=item) if data.is_valid(): entity = data.save(commit=False) entity.put() self.redirect('/') else: self.form() def form(self): self.response.out.write('<html><body>' '<form method="POST" action="/edit">' '<table>') self.response.out.write(MyForm(instance=item)) self.response.out.write('</table>' '<input type="submit">' '</form></body></html>')Programming Language: Python
<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.