[mod_python] newbie question

Robert Brewer fumanchu at amor.org
Wed Oct 27 13:06:15 EDT 2004


Golawala, Moiz M wrote:
> I am very new to mod_python (I have developed application 
> with python but am new to web development in general). I have 
> read most of the documentation and I have a couple of 
> questions. I am planning to use mod_python along with Cheetah 
> to build an internal website for my company. So far I have 
> include some basic handlers in apache and can view a hello 
> world script on my browser. Now when I try to do something a 
> little more complicated I am lost.
> 
>  I have a couple of text fields that I want the user to fill 
> out and then hit the submit button. I can get that html page 
> up but I don't understand the mechanism where once a user 
> hits the submit button, how does he/she get to the next page 
> on the site. 
> 
> In the documentation I don't see any multi page working 
> examples. Can some one point me to some resource that has 
> working mult-page html (PSP or Cheetah based) examples with 
> the necessary Directory tags in the httpd.conf file so I can 
> better understand what is going on. 

In a simple application, you can simply examine req.uri in your
handler() function, and dispatch based on that. Here's an untested
example:


from mod_python import apache, util


def handler(req):
    # req.uri = the path portion of the URI.
    # "http://www.mycorp.org/myapp/topic.htm?a=3" -> "/myapp/topic.htm"
    page = req.uri
    if page == "/myapp/submit.htm":
        return submit(req)
    else:
        return view(req)

def view(req):
    req.write("""<html>
<body>
<form action='/myapp/submit.htm' method='POST'>
Please enter your name:
<input type='text' name='name' size='20' />
</form>
</body>
</html>
""")
    return apache.OK

def get_params(req):
    final = {}
    params = util.FieldStorage(req, 1).list
    if params is not None:
        for p in params:
            final[p.name] = p.value
    return final

def submit(req):
    params = get_params(req)
    req.write("Thank you, %s!" % params.get('name'))
    return apache.OK


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org



More information about the Mod_python mailing list