Simple Google App Engine tutorial using webapp2 and jinja2 in python 2.7
Things are continued from here...
In this post we shall be making a simple application that takes notes. It is done so as to perform CRUD operation via the webapp2 framework.
We want things to be the django way(as per the initial post also, it helps being more organized), following are the requisites:
- Views - to serve the business logic.
- Models - for the database creation.
- URL Conf - to act as url routers.
- Template - the html files
- Static Directory - to save js, css and image files.
Download the Google Appengine SDK for Python, from here. Once downloaded extract the folder, inside google_appengine directory make a project root folder, i name the folder webapp2.
The code is available here, keep it open aside, in case you want to peep.
A Python app specifies runtime configuration, including versions and URLs, in a file named app.yaml. Lets first take up the app.yaml file: to start of you need to mention your application id. Follow this link to create an application.
At the project level, we need to mention the location of static files, the url routing script and any libraries that we would be using.
- url: /static
- url: /.*
- name: jinja2
- name: markupsafe
With the above code we have specified the location of static(js, css, image) files, that we will be using jinja2 template engine and our url router is a method named app in the file main.py..
Lets decide on the database schema. We need a table called Notes, which will have some text, it will have an author, a priority, a creation date and status. All this is done in models.py. The major chunk of our code resides in views.py. Lets take each of the function one by one.
Display all the notes: Make a query to the Notes model and send a dictionary of data which are rendered through the templates. This is achieved in MainPage.
Create a note: If the user has opened the page, send him the form, else if he submits it, put the data in the form. This is done in CreateNote.
Edit a note: Get the id of the note the user wants to edit, if the request type get send him the current data in a form. If the request type is post save the data in the form. The function that does it is EditNote.
Delete a note: Well if the user wishes call DeleteNote.
All these functions are called based on calling of urls, this url and function mapping is done in main.py.
Few things that could have been done better are using a form
framework and probably client/server side form validation, using the
builtin appengine user module, but the objective was to demonstrate
basic CRUD operations using
webapp2 the django way.