16 August 2010

Tutorial about making forms in AppEngine with Python. First part.

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

Here you learn how to make forms for AppEngine in Python.

Make The Model:

First make a model, for forms data and then let django generate from from it. Here I have made model with some random fields.
Source code viewer
  1. class MyModel(db.Model):
  2. title = db.StringProperty(required=True)
  3. type = db.StringProperty(choices=['Earth','Dark','Light','Water','Fire','Wind','Divine'])
  4. year = db.IntegerProperty()
  5. review = db.TextProperty()
Programming Language: Python
There are many kinds of form fields. As you can see they are called properties. You can find the full list of properties from Googles: Types and Property Classes. As you can see there are also parameters, those are form validation parameters. Full list of those you can find from Googles: The Property Class.

Make The Form

This code makes the form from the model. This is made by the django library.
Source code viewer
  1. class MyForm(djangoforms.ModelForm):
  2. class Meta:
  3. model = MyModel
Programming Language: Python

Our AppEngine Form in Python

Lets print our form to the screen. The complete code:
Source code viewer
  1. import os
  2. from google.appengine.ext import webapp, db
  3. from google.appengine.ext.webapp.util import run_wsgi_app
  4. from google.appengine.ext.db import djangoforms
  5.  
  6. # Here goes the model.
  7.  
  8. # Here goes the form.
  9.  
  10. class MainPage(webapp.RequestHandler):
  11. def get(self):
  12. self.response.out.write('<html><body>'
  13. '<form method="POST" action="/">'
  14. '<table>')
  15. self.response.out.write(MyForm()) #generates form
  16. self.response.out.write('</table>'
  17. '<input type="submit">'
  18. '</form></body></html>')
  19.  
  20.  
  21. application = webapp.WSGIApplication(
  22. [('/', MainPage)],
  23. debug=True)
  24.  
  25. def main():
  26. run_wsgi_app(application)
  27.  
  28. if __name__ == "__main__":
  29. main()
Programming Language: Python
Now that the "AppEngine Forms in Python" tutorial is complete you can check out the second part: Submit AppEngine Forms Using POST in Python.