Home arrow Zope arrow Page 7 - The Number Game

Adding It All Up - Zope

Despite what you might think, it's fairly easy to write MySQL-based Web applications with Zope. In this case study, find out how to do just that by building a Zope-based online poll which uses MySQL to store poll questions and user responses, and DTML Methods to extract and present this information.

  1. The Number Game
  2. Reading The Funnies
  3. Start Me Up
  4. Rocking The Vote
  5. Down Memory Lane
  6. Cookie-Cutter Code
  7. Adding It All Up
By: Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 7
April 16, 2002

print this article


The final item on today's menu is perhaps the simplest - a form which allows administrators to easily add new questions to the system.

This DTML method, aptly named "addPoll", is divided into two sections, as seen in the listing below:

<dtml-var standard_html_header> <h3>Add New Poll</h3> <dtml-if submit> <dtml-call insertPoll> <h4>Poll added successfully!</h4> <p> Click here to <a href="viewCurrentPoll">view</a> it. <p> <dtml-else> <table border="0" cellspacing="5" cellpadding="5"> <form action="addPoll" method="post"> <tr> <td>Poll question<br><input type="Text" name="question" size="25"></td> </tr> <tr> <td>Response 1<br><input type="Text" name="response1" size="25"></td> </tr> <tr> <td>Response 2<br><input type="Text" name="response2" size="25"></td> </tr> <tr> <td>Response 3<br><input type="Text" name="response3" size="25"></td> </tr> <tr> <td align=center><input type=submit name=submit value="Add Poll"></td> </tr> </form> </table> </dtml-if> <dtml-var standard_html_footer>
The first task is to check if the form has been submitted. If it has not yet been submitted, then the method generates an empty form, with fields for the question and possible responses. Once the form is submitted, the data entered into the form by the administrator is inserted into the database via the "insertPoll" Z SQL Method below.

INSERT INTO poll (question, response1, response2, response3, date) VALUES (<dtml-sqlvar question type="string">, <dtml-sqlvar response1 type="string">, <dtml-sqlvar response2 type="string">, <dtml-sqlvar response3 type="string">, <dtml-sqlvar ZopeTime type="string">)
Note that the built-in ZopeTime DTML variable (which returns the current system time) has been used to insert the current date as part of the table record. This is similar to the now() function that is built into the MySQL database.

Here's what the form and its result look like:

And that's about it. Hopefully, this exercise gave you some insight into how Zope can be used to build a simple Web application, and illustrated its power and flexibility as a rapid development tool for the Web medium. See ya! Note: All examples in this article have been tested on Linux/i586 with Zope 2.5. Examples are illustrative only, and are not meant for a production environment. Melonfire provides no warranties or support for the source code described in this article. YMMV!

>>> More Zope Articles          >>> More By Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Creating Zope Products
- Plone Content Types With Archetypes
- Flat User Management in Zope
- Creating Basic Zope Applications
- Getting started with Zope for Linux and Sola...
- ZPT Basics (part 4)
- ZPT Basics (part 3)
- ZPT Basics (part 2)
- ZPT Basics (part 1)
- Exception Handling In DTML
- DTML Basics (part 4)
- DTML Basics (part 3)
- DTML Basics (part 2)
- DTML Basics (part 1)
- Using Zope With Apache

Developer Shed Affiliates


Dev Shed Tutorial Topics: