Let's check out that first page!

The code is in place... Point your browser to http://localhost:8080/ and let's see what we've got!

Oh, we've got an error. Since we're in development mode, CherryPy gives us the whole traceback, which is very convenient. The traceback is telling us that we got an SQLObjectNotFound exception. D'oh! We forgot to put a page in the database! Let's do that using CatWalk, the model browser

tg-admin toolbox

A new browser window will open with the TurboGears toolbox. Click on the CatWalk model browser. You'll see "Page" listed on the left. Click on that, select the "Add Page" tab and create a page with the name "FrontPage" and with whatever text you want for the data.

That's all there is to it. We just created a new page in the database.

Reload your browser window, and you'll see our beautiful page.

How do we make a better page?

One of the hallmarks of wikis is that you can edit the page just by clicking "Edit This Page". That and the wikispam that follows... hopefully, spammers won't find your Wiki 20 before we're done with it!

Let's start by creating a template for editing. Let's start with a copy of "page.kid":

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

In edit.kid, change the text at the top from "Viewing" to "Editing" and replace the <div> for the data with:

We need something to use this template now. We'll add an "edit" method to our controller:

This method is very similar to our index method. The main difference is that the index method renders the wiki content as HTML, and this one returns the straight text.

We need a way to get to our edit method, so we'll edit page.kid to add this to the bottom:

That should do it! Reload the page in your browser, and you'll see the edit link. Follow that link, and you'll get a page that lets you edit. Don't click that save button, yet! We still need to write that method.

Saving our edits

The form we made in the last section had an action of "save". So, we need to make a method called save in our controller. Here's what it looks like:

The submit parameter is supplied because it will be passed by the form. If we don't supply this, Python will complain about an unexpected parameter. An alternative is to use **keywords.

Interesting things to note about this:

  1. Unlike the previous methods we've made, this one is just exposed without any template specified. That's because we're only redirecting the user back to the viewing page.
  2. A transaction is set up for us implicitly, if our database supports transactions. You can see for yourself... if you add "raise ValueError" after page.data is set in that method, you'll see that the data is not actually saved in the database. If no exception is raised, the data is committed. If an exception is raised, the changes are rolled back. The exception to this rule is with redirects: they short-circuit execution of the method, but they don't count as "errors."
  3. The "page.data = data" is all it takes to run the UPDATE SQL statement.
  4. The turbogears.flash() call is setting a notification message to appear in the user's browser on the next screen. You'll see a reference to the "tg_flash" variable in the master.kid template.
  5. Since an redirect is raised as an exception, all other processing is short circuited. Very handy!

Go ahead and try it out! You can make changes and save the page we were editing. Pretty wiki-like, no?

Previous Page | Continue on to page 4