Some of the information here may be outdated, please check the book instead

Just for fun. Inspired by this crash course in Django. It is a little different because we decided to:

  • move some of the business logic from the view of the Django example into the controller and to define the comment table explicitly instead of using a plugin.
  • enable markdown WIKI syntax in both blog posts and comments
  • require login for commenting

Download and install

There is no installation process. Download web2py from, unzip and click on "web2py".

It will ask you to choose a one time administrator password. Choose one.

Create a new app

Visit the admin interface at

Login with the password you choose above. In the textbox on the right type the name for the new app, for example "blog", and press "submit". You will be redirected to a design page that let you edit the new page.

Click on "database administration" to get a web based interface to your database.

Create the model

In the design page create a new model file called "" by typing the name in appropriate textbox. In the file write:



Go to the database administrative interface to add some posts:

IMPORTANT!!! Make sure you register and login with the application else posts will have no author and they cannot be displayed.

Edit the controller

In the design page edit the controller "" by clicking on the appropriate link, edit the "index" function and create a "view_post" function as show below:

def index():
    return dict(posts=db().select(

def view_post():
    post =[request.args(0)] or redirect(URL(r=request,f='index'))
    if auth.is_logged_in(): = =
        form = crud.create(db.comment)
        form = A("login to comment",_href=URL(r=request,f='user/login'))
    comments = db(
    return dict(post=post, form=form, comments=comments)

At this point the app is fully working although you may not like the default views so...

Adding Views

Edit the view "default/index.html" associated to the "index()" action. Replace the content of the view with:

{{extend 'layout.html'}}
{{from gluon.contrib.markdown import WIKI}}
{{ for post in posts:}}
     <a href="{{=URL(r=request,f='view_post',}}">
 {{ pass }}

Create now a view for a post and its comments. Do this by creating a new view file "default/view_post.html"

 {{extend 'layout.html'}}
 {{from gluon.contrib.markdown import WIKI}}
 <h2>by {{}}</h2>
 {{for comment in comments:}}
     <em>by {{}}</em>
 {{ pass }}

The End

You can now visit your blog at

and view the individual blog pages at

Commenting requires login. You can login, register, manage your profile, etc here:

You can administer you blog (manage accounts, create and delete posts and comments) at

You can edit your source code online via

If you do not like the default URLs you can change the mapping by editing

You can also do reverse url mapping so that you do not need to edit code or views when url changes. Links will not break.

Google App Engine

This code works on GAE as it is. It does not need any tweaking. You need to run the source version of web2py, not the binary distribution. You must edit the provided "app.yaml" in the main web2py folder and replace "web2py" with the name of your GAE application-id. Then deploy it on GAE

cd /path/to/where/web2py/is update web2py

You can now find your app running at
© 2008-2010 by Massimo Di Pierro - All rights reserved - Powered by web2py - design derived from a theme by the earlybird
The content of this book is released under the Artistic License 2.0 - Modified content cannot be reproduced.