[mod_python] Tutorials, FieldStorage and sys.py. A depressing saga.

Terry MacDonald terry at i3technologies.co.uk
Thu May 22 19:36:24 EST 2003

Hi again,

Thankyou very much for your replies, You seem a nice bunch in here. 
I look forward to conversing with you over time (hopefully not all bug related!)

Anyways your advice solved one issue but unfortunately moved me on to the next.  
I am reaching the end of my patience as I have tried various work arounds to my 
ills and I just go up more garden paths to more problems (this should not be that 
difficult I know!).

Forgive me but I have decided to post the code to see if you can tell what is wrong 
cos I just can't get it to work as it is supposed to. The code is primarily example 
code but has been rejigged a little while trying to get it to work.

Problems occuring now is that the subn function is giving a mulitple repeat error.
When I comment out the offending code I just get Object not found in the browser. 
Also I have to put full pathnames in for the html files otherwise they cannot be 
found (I know this can't be right - but i'm trying anything to get this to work)

Ultimately the example should print a form, fetch the contents and display them 
back to you in another page (I think!) not exactly rocket science.

Below is the .py file and the two basic html files. Enjoy !



from mod_python import apache
from mod_python import util
import re
# specify the filename of the template file
TemplateFile = "/var/www/html/python/template.html"
# Display  takes one parameter - a string to Display
def Display(Content):
    TemplateHandle = open(TemplateFile, "r")  # open in read only mode
    # read the entire file as a string
    TemplateInput = TemplateHandle.read()
    TemplateHandle.close()                    # close the file
    # this defines an exception string in case our
    # template file is messed up
    BadTemplateException = "There was a problem with the HTML template."
    SubResult = re.subn( "<!-- *** INSERT CONTENT HERE *** -->", Content, TemplateInput )
    if SubResult[1] == 0:
        raise BadTemplateException
    print "Content-Type: text/html\n\n"
    print SubResult[0]
def ProcessForm(req):
    form = util.FieldStorage(req)
    # extract the information from the form in easily digestible format
        name = form["name"].value
        # name is required, so output an error if
        # not given and exit script
        Display("You need to at least supply a name. Please go back.")
        raise SystemExit
        email = form["email"].value
        email = None
        color = form["color"].value
        color = None
        comment = form["comment"].value
        comment = None
    Output = ""  # our output buffer, empty at first
    Output = Output + "Hello, "
    if email != None:
        Output = Output + "<A HREF=mailto:" + email + ">" + name + "</A>.<P>"
        Output = Output + name + ".<P>"
    if color == "swallow":
        Output = Output + "You must be a Monty Python fan.<P>"
    elif color != None:
        Output = Output + "Your favorite color was " + color + "<P>"
        Output = Output + "You cheated!  You didn't specify a color!<P>"
    if comment != None:
        Output = Output + "In addition, you said:<BR>" + comment + "<P>"
### Begin actual script
#### "key" is a hidden form element with an
### action command such as "process"
#    key = form["key"].value
#    key = form["key"].value
#    key = None
#if key == "process":
#    ProcessForm(form)
#    DisplayForm()
# Open and display the form
FormFile = "/var/www/html/python/form2.html"
FormHandle = open(FormFile, "r")
FormInput = FormHandle.read()


<FORM METHOD="POST" ACTION="sample.py/processform">
      <INPUT TYPE=HIDDEN NAME="key" VALUE="process">
      Your name:<BR>
      <INPUT TYPE=TEXT NAME="name" size=60>
      Email: (optional)<BR>
      <INPUT TYPE=TEXT NAME="email" size=60>
      Favorite Color:<BR>
      <INPUT TYPE=RADIO NAME="color" VALUE="blue" CHECKED>Blue
      <INPUT TYPE=RADIO NAME="color" VALUE="yellow">No, Yellow...
      <INPUT TYPE=RADIO NAME="color" VALUE="swallow">What do you mean, an African or European swallow?
      <TEXTAREA NAME="comment" ROWS=8 COLS=60>
        <INPUT TYPE="SUBMIT" VALUE="Okay">

    <META NAME="keywords" CONTENT="blah blah -- your ad here">
    <title>Python is Fun!</title>
                <!-- *** INSERT CONTENT HERE *** -->

More information about the Mod_python mailing list