Table Of Contents

Previous topic

20 Minute Wiki Page 4

Next topic

20 Minute Wiki Page 6

20 Minute Wiki Page 5

Adding a page list

Our wiki is functional, but we’ll add a few more features to make it nicer and because we have more TurboGears features to demo.

Most wikis have a feature that lets you view an index of the pages. We can add that to our wiki easily, so let’s do it!

We’ll start with a new template, pagelist.kid. Again, we’ll start with page.kid so that we don’t have to write the boilerplate.

cd wiki20/templates
cp page.kid pagelist.kid
cd ../..

Our pagelist.kid will look like:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns=""
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"
<title> Page Listing - 20 Minute Wiki</title>
    <div id="main_content">
        <h1>All Of Your Pages</h1>
            <li py:for="pagename in pages">
            <a href="${tg.url('/' + pagename)}"
                py:content="pagename">Page Name Here.</a>

Feel free to add “Are Belong To Us” to the heading, if you feel you must.

Just as with the other templates, you can open pagelist.kid directly in a browser. You can see that we’ll get a bulleted list of links to the pages. This is our first use of py:for, and you can see it’s straightforward. The li element will be repeated for each iteration of the for loop, and the for loop is specified just as it is in Python.

Let’s add a link at the bottom of the master.kid template so that we’ll get our page listing link at the bottom of every page:

<!-- End of main_content -->
<div id="footer">
<p>View the <a href="${tg.url('/pagelist')}">complete list of pages.</a></p>
<img src="/static/images/under_the_hood_blue.png" />
  <p>TurboGears is a open source front-to-back web development
    framework written in Python</p>
  <p>Copyright &copy; 2006 Kevin Dangoor</p>

Since we’re referring to a pagelist method, we should probably create it:

def edit(self, pagename):
    page = Page.byPagename(pagename)
    return dict(page=page)

def pagelist(self):
    pages = [page.pagename for page in]
    return dict(pages=pages)

def save(self, pagename, data, submit):

Now you see that this page listing is just a contrived reason to introduce you to SQLObject’s querying facilities (tricky aren’t we). This particular use of isn’t particularly fancy. We’re selecting all of the Page objects from the database, and ordering them by pagename. returns an iterable SelectResults object, which allows us to snag the titles via list comprehension.

The Page.q.pagename represents the pagename field of the underlying table rather than any particlar Page object’s pagename attributes. The underlying fields are only used doing queries, so the .q is a mnemonic for “query”.

The select method attached to your object classes provides methods to construct almost any query you’d like to run on them. Check the sqlobject docs for more details.

You can see your pagelist by clicking the link at the bottom of your pages or going directly to http://localhost:8080/pagelist.

Go back to Page 4 | Continue on to page 6