Web2py tutorial to create a Notes Application
In this post lets talk about the web2py python framework. This post is NOT about how web2py compares with other python frameworks, if you are interested in say web2py vs django follow this, this or this.
This post is about creating a simple database driven web app, that performs CRUD operations using the web2py framework. The source code for the application can be found here.
Web2py is a python web framework and was created by Massimo Di Pierro along with a community of professionals and University professors in Computer Science and Software Engineering. The first version came out in 2007. The good parts have been described here.
Lets get our hands dirty. The application we are going to develop is a simple note taking application, a user can create notes, edit and delete them. The index page will show all the notes. I would assume you have the pre-requisites as python installed. You begin by downloading the framework. Open your terminal and go to the directory where you have downloaded the package. Run python web2py.py. You will see a splash screen, enter a session password.
Through the admin inerface, create a new application (on the right you see New Application Wizard). I name it web2pynotes. Once done, on the left would appear a folder structure with web2pynotes at the top. Click edit. We are now ready to write our models views and controllers. If you don't yet understand what an MVC is, you might want to read this simple article.
Web2py provides a really good Database Abstraction Layer. In short a database abstraction layer is something which converts your python code to tables, rows and columns in the database. You can read more about it here. Lets decide our database model, we need to have a table Notes, with the columns: Title, Description, Subject, Author and a publication date. Thats it, lets create our model first, edit your db.py the relevant code can can be found towards the bottom of the page.
We need the following functionality:
- A function to Display all the notes (index)
- A function to Create Notes (create)
- A function to Edit the Notes (edit)
Click on the edit notesweb2py and then click the edit default.py in controllers, the relevant code with comments describing the functionality can can be found here.
Views or the Templates
Go to the views and enter default/create.html to create a file which will render the create form and we need to edit the default/index.html. The files with code can be found here.
Once done, http://127.0.0.1:8000/web2pynotes/default/index (rename web2pynotes to the name of your project) and you can see it live and running.
Following slideshow summarizes and shows visually the steps that we have followed:Previous Post Next Post