Lesson 12. The Elvis Files, cont.

Zope cannot find the tutorial examples. You should install the tutorial examples before continuing. Choose "Zope Tutorial" from the product add list in the Zope management screen to install the examples.

If you have already installed the tutorial, you can either follow along manually, or reinstall the tutorial examples. Note: make sure that you have cookies turned on in your browser.

In the last lesson you created an Elvis sightings database. Now let's adapt the this database to allow site visitors to submit their own sightings.

This lesson builds on your experience from the last two lessons. To allow folks to report sightings you'll need a report form just like you used in lesson 10.

  1. Click on the form.html template to view its contents.

This page is almost exactly like the report form used in Lesson 10. It collects data and calls the action.py script to process the data.

  1. Click on the action.py Python Script to view its contents.

It calls the insertSighting ZSQL Method with this code:

insert(name=name, location=location, date=date,
       description=description)

Then it returns a confirmation page.

The real work is done by the insertSighting ZSQL Method. Let's see how.

  1. Click on the insertSighting ZSQL Method to view its contents.

The query template contains the following SQL code:

insert into elvis_sightings
  values(
<dtml-sqlvar location type="string">,
<dtml-sqlvar date type="string">,
<dtml-sqlvar name type="string">,
<dtml-sqlvar description type="string"> 
)

This code inserts a row into the elvis_sightings table. Notice how the arguments of this ZSQL Method correspond to the form elements in the form.html template.

You can test this SQL code to make sure it works correctly.

  1. Click on the Test tab.
  2. Enter some sample data into the automatically generated form.
  3. Click the Submit Query button.

Zope will send the SQL code to the database and will tell you the SQL code that was sent, and the results that the database returned. In this case the database will not return anything, since the SQL INSERT command produces no results.

Now return to the sightings.html page to see if the sample data was added to the list of sightings. Play with the system a little bit and see how the links between the sightings.html, form.html, and action.py objects allow a visitor to navigate the site.

Summary

You can easily change data in a relational database with Zope. The process for inserting data is very similar to the process for querying a database; you use a ZSQL Method in both cases.