Lesson 11. The Elvis Files

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.

Everyone knows that serious web sites use databases. We agree. Let's convert our Elvis sightings system to use a relational database. This will provide us with scalability and interoperability.

Let's see how Zope gets data from a database.

  1. Click the connection Database Connection to edit it.

A Database Connection tells Zope how to access a relational database.

  1. Click the Browse tab to examine the database tables.
  2. Click the plus box next to the ELVIS_SIGHTINGS table to expand it.

Now you can see the column names and types for this table. Now let's see how to get information out of this table.

  1. Click the getSightings ZSQL Method in the lesson11 folder to edit it.

Notice that the SQL statement used to fetch information about Elvis sightings is editable in the Query template field.

  1. Click the Test tab.
  2. Click the Submit Query button.

This shows you what data the ZSQL Method returns.

A ZSQL Method is used to get information out of or into a relational database.

Now let's see how to use this ZSQL Method in a web page.

  1. Click the sightings.html template in the lesson11 folder to edit it.

Notice that the sightings are generated by iterating over the results of the getSightings ZSQL Method with the tal:repeat statement.

  1. Add a line after each sighting by changing the contents of the sightings.html template to:
    <html>
    
    <div tal:repeat="sighting container/getSightings">
    
    <p><span tal:replace="sighting/date">date</span> -- 
       <span tal:replace="sighting/location">location</span>
    </p>
    
    <p>Reported by <span tal:replace="sighting/name">name</span>
    </p>
    
    <p tal:content="sighting/description">description
    </p>
    
    <hr>
    </div>
    
    </html>
  2. Click the Save Changes button.
  3. Click the Test tab to view the page.

Summary

Zope can work with data from relational databases in the same way it treats other Zope objects.