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


Since web2py 1.63 we made it very easy to return data in formats other then HTML, upon request. For example, consider the following action in controller "default" on app "app"

def count():
    session.counter=(session.counter or 0)+1
    return dict(counter=session.counter,

(this action returns a counter that is increase when the visitor reload the page, and the timestamp of the current page request)

Normally this page would be requested via

Since web2py 1.63 the same page can also be requested as

And the dictionary will be rendered in HTML, XML and JSON respectively.

How it works

Other extensions can be easily defined by the user. When the .xxx extension in called web2py looks for a template file called default/ and if it does not find it, it looks for a template The files generic.html, generic.xml, generic.json and generic.rss (described below) are provided with the latest scaffolding app.

There is nothing to do to enable this in new web2py app. To use it in old/existing web2py app just copy the generic.* files from the scaffolding app.


Here is sample generic.pickle that can return the dict pickled

import cPickle

which allows visitors to call


To generate an RSS feed using this method, web2py provides a generic.rss but it only works if the action returns a dictionary containing a "title", "link", "description", and "items" where items is a list of dictionaries each with a "title", "link", "description". For example

def feed():
    return dict(title="my feed",
                description="my first feed",
                  dict(title="my feed",
                       description="my first feed")

This action can be accessed as

but also as


Yes, of course it works on the Google App Engine too.

Yes, they are bytecompiled when you press the "compile" button in the admin interface

Yes, you can cache them if you modify the action:

def count():
    session.counter=(session.counter or 0)+1
    return response.render('default/count.'+request.extension,d)
© 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.