Next / Previous / Contents / TCC Help System / NM Tech homepage


Describes the techniques for generating Web pages dynamically, especially from the XHTML forms interface.

This publication is available in Web form and also as a PDF document. Please forward any comments to

Table of Contents

1. Introduction: Why do we need this technique?
2. References
3. Linking to a CGI script
3.1. Linking a form to a CGI script
3.2. Direct CGI links without use of a form
4. Writing your CGI script
4.1. The cgi module
5. anyform.cgi: An example CGI script to show all form data
5.1. XHTML to be generated
5.2. Prologue
5.3. main(): The main program
5.4. showControl(): Display one control's values
5.5. Epilogue
6. Cookies: Do I know you?
6.1. Design considerations for CGI with cookies
6.2. Cookie operations
7. reader.cgi: An example script with cookies
7.1. Design notes for reader.cgi: Sequencing
7.2. The user database
7.3. Classes used in this application
7.4. reader.cgi: Prologue
7.5. Imported modules
7.6. Manifest constants
7.7. main(): Main program
7.8. deleteCase(): Delete the cookie
7.9. deletionPage(): Generate the deletion-successful page
7.10. genericPage(): Build a generic Web page
7.11. chapterCase(): Present the next chapter
7.12. updateDatabase(): Update or create the user's database entry
7.13. newUserRecord(): Create the initial user record
7.14. setCookie(): Set a cookie
7.15. generateChapter(): HTML page generation
7.16. generateForm(): Build the form element
7.17. navButtons: Add navigational buttons
7.18. radioGroup(): Add the radiobutton group
7.19. class Inputs: All the script's input
7.20. Inputs.__init__(): Constructor
7.21. class UserDatabase
7.22. UserDatabase.__init__(): Constructor
7.23. UserDatabase.__getitem__(): Implement dictionary get
7.24. UserDatabase.__setitem__()
7.25. UserDatabase.__delitem__()
7.26. UserDatabase.cleanup()
7.27. UserDatabase.close()
7.28. class UserRecord
7.29. Epilogue

1. Introduction: Why do we need this technique?

Most Web pages are static: the same content appears each time you visit that URL. To put up static content, you simply place a file in or under a specific directory, and give it a name that ends in “.html”. If your page is hosted at the New Mexico Tech Computer Center (TCC), static pages must reside in subdirectory “public_html” under your home directory.

This document describes technique for dynamic web pages: their content is generated on demand. This technique is called CGI, for Common Gateway Interface. Instead of writing the page's content, you will write a program that generates the content as its output. Such a program is called a CGI script (nowadays the terms “script” and “program” are pretty much interchangeable). The program can be written in just about any modern programming language. We'll describe how to do it in the Python language, a very good modern general-purpose programming language.

One of the commonest uses for dynamic web pages is to handle forms. For example, you might put up a web page displaying a form that allows the user to enter search terms for a database query. When they click on the Submit button, a new page appears that displays the results of their query.

However, dynamic web pages are useful for other applications besides forms handling. For example, you might put up a “Site Map” link that takes the user to an interactive site map. An example is the site map for the TCC Help System.