mod_python newbie question.

  • Thread starter Golawala, Moiz M (GE Infrastructure)
  • Start date
G

Golawala, Moiz M (GE Infrastructure)

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.

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.

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>

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

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.

Again, any help is appreciated.
Thanks
Moiz Golawala


Moiz Golawala
GE Infrastructure, Security
Software Engineer
Enterprise Solutions

T 561 994 5972
F 561 994 6572
E (e-mail address removed)
www.gesecurity.com

791 Park of Commerce Blvd., Suite 100
Boca Raton, FL, 33487, U.S.A.
GE Security, Inc.
 
S

Steve Holden

[posted and mailed]

Golawala said:
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
 
T

Tim Roberts

Golawala said:
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.

If you are new to web development, you should throw out mod_python and
create your internal web using using plain, ordinary CGI scripts.
Mod_python introduces a number of configuration complications and debugging
difficulties (as you have already found). The performance penalty of
invoking a new interpreter each time is very slight, especially if you're
compiling Cheetah templates each time. CGI scripts are easy to debug, and
unlike mod_python handlers, you can test them from a command line.

Remember the golden rule of development: FIRST get it to work, THEN decide
if it needs to go faster. Many production web sites work just fine with
nothing fancier than CGI.
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.

By switching to CGI, you lose all of this configuration file mucking. Just
add Options +ExecCgi.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top