mod_python newbie question.

Steve Holden steve at holdenweb.com
Thu Oct 28 00:34:53 EDT 2004


[posted and mailed]

Golawala, Moiz M (GE Infrastructure) wrote:
> Hi All, 
> 
> 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.
> 
You've got all the bits, you just haven't managed to put them together 
the right way yet. Don't worry, there's not much wrong. The main thing 
you've spotted (but failed to understand the implications of) is that 
all .py references to that directory trigger the same script.

You are actually writing an Apache handler rather than application code. 
It's a tribute to mod_python that the likes of us can achieve this, but 
in fact we can usually stay away from handlers, because Grisha was kind 
enough to provide the basics as a part of mod_python.

>  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. 
> 
The handler calls the appropriate functionality is how it's supposed to 
work.

> 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. 
> 
It isn't easy to find, but if you read 
http://www.modpython.org/live/current/doc-html/hand-pub-alg-trav.html 
carefully it explains rather succinctly how the publisher handler works. 
The sections that follow give you a bit more detail.

> Here is an some code to explain what I am trying to do and what my confusion is:
> 
> In my httpd.conf file:
> <Directory /var/www/html/>
> 	PythonHandler main
> 	PythonDebug on
> </Directory>
>
I'm not an Apache configuration expert, but I think this means every 
reference to the home directory will be processed by main.py, no matter 
what the URL.

I think (personally) you'd be better off starting with the pre-written 
"publisher" handler.

> in my main.py file:
> 
> from cheetah.Template import Template
> from mod_python import apache
> 
> def handler(req):
> 	t = Template(file,"/var/www/html/main.tmpl")
> 	textlen = len(str(t))
> 	req.set_content(t)
> 	req.set_content_length(textlen)
> 	apache.OK	
> 
A very basic handler, but unfortunately as you have discovered, it gets 
triggered for every request. The "publisher" handler can be used to call 
one of a number of routines inside a single page by "decoding" the URL 
to decide which resource to call in a specific Python module.

> in my main.tmpl file:
> 
> <html>
> <head></head>
> <body>
> 	<form action="valid.py">
> 	<input type="text" size="20" name="input1">
> 	<input type="text" size="20" name="input2">
> 	<input type="submit" name="submit" value="submit">
> 	</form
> </body>
> </html>
> 
> My problem is that the browser renders the page that is defined in the main.tmpl file. But once I enter the text and click submit. I want the browser to display the page that is defined the template used in valid.py (as the form tag shows) but some how I get the main.py page back. The reason this is happening is that once I click the submit button, I get back to the server and the handler is invoked and main.py is called. I can never go to another page. 
> 
> I am sure I am doing something wrong. Please can someone help me out.. Or direct me to some place that show an example of how this mechanism works correctly.
> 
Well, instead of your current configuration, try something like:

<Directory /var/www/html/>
	AddHandler mod_python .py
	PythonHandler mod_python.publisher
	PythonDebug on
</Directory>

This will have the publisher handler process the calls. Since you've 
already got a "main.py", let's keep that file but modify it slightly. 
Note that this code is untested, so you may have to fiddle with it:

from cheetah.Template import Template
from mod_python import apache

def index(req):
	return Template(file,"/var/www/html/main.tmpl")

def doForm(req, input1, input2, submit):
	return ...

You will need to access the form with the URL

     http://yourserver/main

although as Grisha explains, you could equally use

     http://yourserver/main/index

The "main.tmpl" file should be modified to make the action of the form

     http://yourserver/main/doForm

One of the slight problems with "implied function" URLs such as the 
first one above is that they bugger up relative links. Ideally your 
template should just use

     <form action="doForm">

but this will fail if you use the first URL, as the browser will 
translate it to

     http://yourserver/doForm

rather than

     http://yourserver/main/doForm

which is what's really required.

> Again, any help is appreciated. 

Let me know how you get on. mod_python is cool!

regards
  Steve
-- 
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119



More information about the Python-list mailing list