Newbie and Page Re-Loading
Miki
miki.tebeka at gmail.com
Fri May 4 17:10:11 EDT 2007
Hello Richard,
> I do not want to run a framework yet. I would like to understand
> python at script level, before adding more aspects to learn, like
> frameworks.
The way CGI works is that your script is called every time the
corresponding HTML is loaded. You can access all the parameters sent
to the script using cgi.FieldStorage.
> I think I get your idea about hidden fields and how to alter them.
Note that hidden fields are passed in plain text format from/to the
server, don't send anything sensitive in them.
> My single page script should work something like this
>
> DisplayHTMLHeaderandBodyHeader
> Check if this is a Re-Load (HiddenField would not exist first time I
> am assuming)
It could be None: cgi.FieldStorage().getvalue("hidden_attribute") ==
None
> Display SearchSection with previous SearchRequest
> If SearchRequest is True: Get and Display Results
> Display TrailerHTMLandTrailerBody
>
> ......... Wait for NewSearch or NextPage
In CGI you don't wait, the script exists and called again when use
hits a button/refresh ...
> Does the above make sense or is there a better way ?
There are many other ways (AJAX, Web frameworks, FastCGI ...). However
I'd recommend you start with plain CGI which is *simple*.
Here is a small example:
#!/usr/local/bin/python
import cgitb; cgitb.enable() # Show errors in HTML output
from cgi import FieldStorage
FUNNY = [ "mickey", "donald", "daisy", "minnie", "goofy" ]
def main():
print "Content-Type: text/html"
print
form = FieldStorage()
query = form.getvalue("query", "")
print '''<html><body>
<h1>Disney Search</h1>
<form>
<input type="text" name="query" value="%s">
<input type="submit" value="search">
''' % query
if query:
for someone in FUNNY:
if query in someone:
print "<br />%s" % someone
print "</form></body></html>"
if __name__ == "__main__":
main()
> How do I get the directory of my modules into the Python Path
import sys
sys.path.append("/path/to/my/modules")
Note that your script directory is automatically added to the path.
> Is there a lightweight Https Server I could run locally (WINXP), which
> would run .py scripts, without lots of installation modifications ?
http://lighttpd.net.
Make sure "mod_cgi" is uncommented, set your document root and set
right python interpreter in cgi.assign
HTH,
--
Miki <miki.tebeka at gmail.com>
http://pythonwise.blogspot.com
More information about the Python-list
mailing list