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

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.

HERE YOU CAN RUN AN INTERACTIVE DEMO ONLINE

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

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

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 default.py, 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

 http://127.0.0.1:8000/MyBlog/default/hello

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

Part II

10) Go back to http://127.0.0.1:8000/admin/default/design/MyBlog

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

 db=SQLDB('sqlite://db.db')
 db.define_table('message',
    SQLField('title',requires=IS_NOT_EMPTY()),
    SQLField('author',requires=IS_NOT_EMPTY()),
    SQLField('email',requires=IS_EMAIL()),
    SQLField('body','text',requires=IS_NOT_EMPTY()))

12) Go back to [design] and edit controller default.py

 def hello():
     form=SQLFORM(db.message)
     if form.accepts(request.vars): response.flash='message posted'
     messages=db().select(db.message.ALL)
     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>
 <body>
 <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!
 {{=form}}
 {{for message in messages:}}
 <p>{{=message.author}} says "{{=message.body}}"</p>
 {{pass}}
 </body>
 </html>

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.