View Single Post
Old October 28th, 2007   #20
smartbei
Has an Ubuntu Drip
 
smartbei's Avatar
 
Join Date: Nov 2006
Location: Israel
Beans: 703
Ubuntu 9.04 Jaunty Jackalope
Re: Making a web page

Well, here is a pretty precise run-down of what is happening. Note - this assumes that you are somewhat versed in python. If not, read up on some python tutorials. This isn't really advanced stuff, language-wise. If there is something specific you don't understand, don't hesitate to ask. I also use html forms to send data, so you may want to check those out (if you haven't yet already).

Before Main:
The line:
Code:
#!/usr/bin/python
Tells whoever is running the program what interpreter to use to process it.

The next line imports cgi, which is a module that I don't know much about, other than the fact that it provides the FieldStorage object so I can access user input through the form.

The main function:
The first line prints the header of the page, telling the browser what to do with the rest of the data it receives. In this case, we are sending html, so we put text/html.

The second line initializes the FieldStorage object, which allows us to access user data sent in a form (or rather any data sent by Get or Post).

The third line stores the value in the FieldStorage object which corresponds with the key "name". This is the same name we give to the html input some lines below. Don't be confused by name (attribute) and "name" (value of said attribute, key to data in FieldStorage object).

The fourth line checks if name (the variable in which we stored the value pointed to by "name") is None, the default value returned by getfirst in case no value was sent (this is the first time the user went to the page, etc.). None is the python equivalent to NULL.

If a name was not None, it processes it and stores the value in the same variable. See below for an explanation on that function.

Otherwise, it sets the variable name to a default value ("No name input :(" ).

Then, we print out all of the html for the page (note - not standards-compliant, just hacked together for the example). The only thing to notice is the %s in the <h1>. This is the python way to do string formatting, %s means insert string at that place. Then, at the end you have one % and the variable you wish to replace it with.

Note that I use a triple quote (""") in order to quote multiple lines.

The nameProcess function:
(takes as input orig, the string to be processed)
The first line initializes the variable that will be returned.

The second line starts a loop (read up on python loops).

The third line adds to final (+=) the current letter (a) and a period (can be whatever).

The last line in the function returns the string final.

Lastly,

Code:
if __name__ == "__main__":
    main()
Checks if this file is the file being run, or whether it is being imported by another. (For example, you may want another file to use this file's nameProcess function, but then you probably wouldn't want the main function to run automatically.

Hope that made things clearer. The best way to learn is to try things out, so go ahead and take this code and try to change it to make use of the function you have. Enjoy! :)
__________________
Intel E6300 / MSI P4M890M / 2GB DDR2 677 / 80GB SATA2 / GeForce 6200TC / DL DVD+-RW / ViewCom 17" LCD

Last edited by smartbei; October 28th, 2007 at 04:30 PM.. Reason: stupid smilies...
smartbei is offline   Reply With Quote