program organization question for web development with python

H

Hans

Hi,

I'm new to this area. Please allow me to ask some (maybe stupid)
questions.

I'm planning to write a web application which used for searching my
mysql database.

1. files organization

I have this in my main.py:
print """<a href="display_tb.py?id=%s&table=%s&cursor=%s">%s</a>""" %
(record[0],table_name,cursor_name,record1)

it kind of works but not comfortable to me.

Do I have to use two files(main.py and display_tb.py)? does that means
each hyper-link needs a single file? Can I put those files together
and then they can share variables,classes, modules, etc?

2. database cursor as parameter?
I created database cursor in my main.py and then I have to use it in
another file(display_tb.py), Can I? I put cursor as a parameter and
try to send it through hyper-link. but it somehow does not work.
error log listed below:

/usr/lib/cgi-bin/display_tb.py in ()
20 sql_str = "SELECT * FROM %s " % search_str_list
21 print "<p>%s</p>" % sql_str
22 cursor_ptr.execute(sql_str)
23 result = cursor_ptr.fetchall()
24
cursor_ptr = '<MySQLdb.cursors.Cursor object at 0xb786b36c>',
cursor_ptr.execute undefined, sql_str = 'SELECT * FROM env_test where
id=20 '

<type 'exceptions.AttributeError'>: 'str' object has no attribute
'execute'
args = ("'str' object has no attribute 'execute'",)
message = "'str' object has no attribute 'execute'"
 
J

James Mills

I'm new to this area. Please allow me to ask some (maybe stupid)
questions.

Without reading the rest of your post too much. Designs are up to you,
I can't comment.

I can only share in a fairly common view, and that is, we'd encourage
you to use a web framework
as opposed to plain old CGI.

cheers
james
 
H

Hans

Without reading the rest of your post too much. Designs are up to you,
I can't comment.

I can only share in a fairly common view, and that is, we'd encourage
you to use a web framework
as opposed to plain old CGI.

cheers
james

Hi James,

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

I read something about web framework like django, but seems it's a
little bit complicated. 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.

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?

Thanks again!

Hans
 
M

MRAB

Hi James,

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

I read something about web framework like django, but seems it's a
little bit complicated. 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.

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?
I recently wrote a web-based program using CherryPy, which was very
straightforward. That might suit your needs.
 
B

Bruno Desthuilliers

Hans a écrit :
(snip)
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
web.py (http://webpy.org/) or flask (http://flask.pocoo.org/).
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.
 
L

Lawrence D'Oliveiro

In message
Hans said:
print """<a href="display_tb.py?id=%s&table=%s&cursor=%s">%s</a>""" %
(record[0],table_name,cursor_name,record1)

I would recommend avoiding filename extensions in your URLs wherever
possible. For executables, in particular, leaving out the “.py†or “.pl†or
whatever makes it easier to switch languages, should you need to in future.
 
H

Hans

Hans a écrit :
(snip)


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


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
  web.py (http://webpy.org/) or flask (http://flask.pocoo.org/).


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






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.

Thank you very much! I got your meaning. The choice 1 is definitely
what I want, I just cannot think about this idea by myself. Thank you
again!
 

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

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top