CGI: Run Script then load page

Sam Penrose sam at ddmweb.com
Tue Aug 7 11:55:24 EDT 2001


Lang (bob at tharin.com) wrote:

> The posted script, of course, works perfectly.  But looking back to my
> original posting, I realize I didn't really articulate my problem very
> clearly.
[and then describes the problem of maintaining state across page loads
in a CGI-based web application]

You're on the right track, but you need to get a bit more modular. The
following is based on what I could glean of what your script actually
does, which you are welcome to state more clearly <wink>.

1) Separate the state of the application (i.e., whether you are storing
submitted-but-not-yet-saved-values in hidden fields) from the
presentation logic (what kind of page the user is looking at). Make
each kind of page an object, or at least the return value of a routine.

2) Use the html "value" (simple) or the "name" (more sophisticated) of
the button to tell the system what to expect. Let's say the user makes
a change that needs to be confirmed. The first button might read:
<input type="submit" name="action" value="Change">. In the main routine
of your script, you check to say:

if form['action'].value == 'Change':
    page = ConfirmationPage()

ConfirmationPage puts their input into hidden fields and returns HTML
with a confirmation message and a button that reads:
<input type="submit" name="action" value="Confirm changes">. The main
routine in your script continues:

elif form['action'].value == 'Confirm changes':
    lastDate = saveInput() #after saving, return state information
    page = MainPage(date=lastDate) # the line you were asking for?

to handle the first page view, before the user has entered any info,
the routine might conclude:

else:
    page = MainPage() # no date passed; use default

HTH,
Sam




More information about the Python-list mailing list