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

Let's say you have a static page called hello.html and you wish to turn it into a Blog. Here is one way to do it.


For the sake of the argument we will assume that the hello.html contains

 <html><head><title>Hello World</title></head>
 <img src="image.jpg"/>
 Please say something here!

Here is what you do:

Part I

1) Download and start web2py from here (no need to install)

2) Choose an admin password and click on [start server]

3) When the browser pops up (automatically) click on [administrative interface]

4) Login with the password you have chosen before.

5) At the bottom fill the form to create a new app, let's call it "MyBlog", and press [submit]

6) Click on MyBlog [design]

7) edit the controller, delete everything in there are add,

 def hello(): return dict()

8) go back to [design] and create a view default/hello.html, paste your hello.html code in there.

9) if your page contains links to static files, for example

 <img src="image.jpg"/>

upload the files under "static files" and edit the hello.html so that links look like this

 <img src="{{=URL(r=request,c='static',f='image.jpg')}}"/>

At this point you should be able to view the page by visiting clicking on hello under Controllers or visit

You are already using web2py server and your page is no longer static.

Part II

10) Go back to

11) Under [Models] create a new model called db and edit the newly created file


12) Go back to [design] and edit controller

 def hello():
     if form.accepts(request.vars): response.flash='message posted'
     return dict(form=form,messages=messages)

13) Go back to [design] and edit the view hello.html and add the lines starting with {{

 <html><head><title>Hello World</title></head>
 <img src="{{=URL(r=request,c='static',f='image.jpg')}}"/>
 {{if response.flash:}}<div class="flash">{{=response.flash}}</div>{{pass}}
 {{='hello user '+request.env.remote_addr}}
 Please say something here!
 {{for message in messages:}}
 <p>{{}} says "{{=message.body}}"</p>

Done! Your page turned into a blog that supports validation of input and prevents injection vulnerabilities by escaping all output. The blog entries are stored in the sqlite database (comes with web2py) but can use MySQL, Postgresql, or Oracle without changing the code.

Where is the SQL? web2py writes it for you. If you really want to see it, click on [sql.log] under [design]

Now you can use the admin interface to manage the blog posts and pack this web2py app for deployment on any web2py installation, including on Google App Engine.

© 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.