Pointing to a database

TurboGears has a minimum of required configuration. It does need to know where your database lives. This can be done in code, but the quickstart creates a couple of simple INI-format config files for you.

Since we're working in a development environment and not a deployment one, edit the "dev.cfg" file. You'll just need to uncomment the sqlobject.dburi line that corresponds to your database and provide the proper connection info.

Restart the web server by hitting control-C and running the startup script again:

python wiki20-start.py

Creating the Database

Since we've created, in Python code, the schema for our simple database and we've also told TurboGears where to look for the database, we're ready to actually create it.

tg-admin sql create

The "tg-admin sql" command is a wrapper around SQLObject's sqlobject-admin command. It looks in the config file to figure out where to find the database.

Let's display a wiki page!

Hard to believe it, but we're already ready to start displaying pages. The first step, is to rename our template. "welcome.kid" just won't do. Rename the template using whatever commands do the trick for your operating system:

cd wiki20/templates
mv welcome.kid page.kid
cd ../..

Now, let's replace the body of the template with something more reasonable for a wiki page:

Notice that you can open page.kid in your web browser, and it is still perfectly viewable. That placeholder text can really help see a layout before it's put in action!

TurboGears greatly reduces the amount of code you need to write, but it does not eliminate it. Let's add a couple of imports to the top of controllers.py:

Then, we'll replace the index method with one that:

  1. Sets the template to our newly named "page" (line 1)
  2. Has a default pagename of "FrontPage" (line 2)
  3. Retrieves the page from the database (line 3)
  4. Formats the text as HTML (line 4)
  5. Deals with unicode properly (always a good habit, on line 5)
  6. Returns the data for the template to use (line 6)

All that in six, very readable lines. The dictionary that we're returning at the end provides the data that populates the template and has other magical powers that we'll see later.

If you get a KeyError on the publish_parts line when you try to access the page, you are using an older version of docutil and should upgrade to the latest.

Continue on to page 3