program organization question for web development with python

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Fri Sep 17 11:36:43 CEST 2010

Hans a écrit :

> Maybe I did not make my question clear. I never tried python web
> programing before, so I want to start from CGI.

You can indeed learn quite a few things doing raw CGI - the most 
important one being why frameworks are a good idea !-)

> I read something about web framework like django, but seems it's a
> little bit complicated.

Not that much IMHO, but being an early django user I'm probably a bit 
biased. Now Python is known as "the language with more web frameworks 
than keywords", so you could probably check some lighter framework like ( or flask (

> my task is actually very simple: get search
> string from input, and then search database, print search result. I
> thought CGI should be good enough to do this.

CGI is "good enough" to do any web stuff - just like assembler is "good 
enough" to write any application !-)

> I don't have any idea about how to organize those cgi codes, so what
> I'm asking is:
> 1. do I have to have each single file for each hyper-link? Can I put
> them together? how?
> 2. how can I pass a db_cursor to another file?   can I use db_cursor as
> a parameter?

Obviously not. FWIW, both questions show a lack of understanding of the 
HTTP protocol, and you can't hope to do anything good in web programming 
if you don't understand at least the basics of the HTTP protocol, 
specially the request/response cycle.

Now for a couple more practical answers:

There are basically two ways to organize your url => code mapping:
1/ have only one cgi script and use querystring params to tell which 
action should be executed.
2/ have one cgi script per action.

The choice is up to you. For a simple app like yours, the first solution 
is probably the most obvious : always display the seach form, if the 
user submitted the form also display the result list. That's how google 
works (wrt/ user interface I mean).

Now if you still need / want to have distinct scripts and want to factor 
out some common code, you just put the common code in a module that you 
import from each script.

More information about the Python-list mailing list