Lecture 58:

posted May 16, 2013, 6:03 AM by Samuel Konstantinovich   [ updated May 16, 2013, 12:54 PM ]
I. UPDATES:
0. Lecture 55 had homework, in fact lecture 55 was just telling you what you should do at home. If you do not follow directions for that homework you will be missing out on FREE POINTS... Think of it like EXTRA CREDIT!!! that if you don't do, you lose points! (That is regular credit)

1. Some of you broke your local web server, here is a newer set of links to use which may help your problem:
For the oblivious: When I say Local Web server... this is what I am talking about.

2. Some of you had issues opening files on the local web server. Instead of putting the file in the same directory as your py file,  put the data files in the same directory as the webserver py file. Then you can open the files as if they were in the same directory as the py files. (Put the py files in cgi-bin, put the data files 1 directory above that, and pretend they are in the same directory)

3. Here is a link to a cgi tutorial which has more examples: http://www.tutorialspoint.com/python/python_cgi_programming.htm
You can look closely on how to handle cgi input. The only funny thing is how they print. They are using a different style print where they substitute things that come after the % symbol. You can print with strings and +'s if you want, its the same effect.


II. Practice Quiz:

On paper: (Read All questions in part II before you write anything)

1. Write an html form (assume the file is form1.html)  that would allow for a checkbox, a text field, and a submit button. They should be labeled and arranged in the format:
    Enter Name: ______________
    [x] I agree to terms of service    
    SUBMIT
The submit button should send the information to the form in question 2. 

2. Write a python file (assume it is named processForm1.py) that gets the information from the form and prints out one of several options:
a. "You didn't enter your name" when the name field was blank
b. Hello ______. Thank you for agreeing. (Their name should go in the blank) This happens when the checkbox is checked.
c. You did not agree!!!! This happens when the checkbox is not checked but the name is filled in.

These examples are exactly how it should work:

http://address/processForm1.py?username=Dave&box=on
Prints the web page:    Hello Dave. Thank you for agreeing.

http://address/processForm1.py?username=Bobbanna
Prints the web page:    You did not agree!!!!

http://address/processForm1.py?box=on
Prints the web page:    You didn't enter your name

3. Immediately upon reading this question, raise your hand to get my attention and give me a thumbs up so I know you followed directions. Do not do this after you completed 1 and 2. If you completed both 1 and 2, just say "Woot!"

III. Prototype your project upgrade.
You have thought about your project's upgraded version and should have some idea what you are doing. 
1. Make the HTML form (it does not need to be connected to anything yet) that you would want to use with you project and describe how you would want it to work. (in the html)
2. Share your ideas with two of your neighbors and get some feedback about what they think. 
3. Think of how you would implement the code to do what you want you can discuss that with neighbors.

IV. Warnings
1. If you get help from an outside source, you better understand the help if you incorporate it into your code. If you copy the code into yours then you don't understand. If you read their help, and you can then write it yourself that means you understand it. You may notice, I always give examples that you cannot generally copy/paste into your code unless its something that you don't really need to understand (like the md5 module) Your goal should be to read/test examples so you understand how to write it yourself. 

V. Solutions:
HTML form:
<form action="processForm1.py" method="get">
   Enter Name:<input type="text" name="username"><br>
   <input type="checkbox" name="box">I agree to terms of service<br>
   <input type="submit" value="SUBMIT">
</form>

Python file 1:
#!/usr/bin/python
print 'content-type: text/html'
print
import cgi
form = cgi.FieldStorage()
if 'username' in form:
    name = form['username'].value
    if not 'box' in form:
        body= 'You did not agree!!!!'
    else:
        body= 'Hello '+name+'. Thank you for agreeing.'
else:
    body= 'You didn\'t enter your name'
header='''<html><head><title>wasup!</title></head>
<body>'''
foot='''</body></html>'''

print header+body+foot

Python file 2:
#!/usr/bin/python
print 'content-type: text/html'
print
import cgi,cgitb
cgitb.enable()
form = cgi.FieldStorage()

#this version uses the getvalue method in the fieldstorage

name = form.getvalue('username')
#this will give None if the key isn't there
box = form.getvalue('box')
if name==None:
    body= 'You didn\'t enter your name'
elif box==None:
    body= 'You did not agree!!!!'
else:
    body= 'Hello '+name+'. Thank you for agreeing.'

header='''<html><head><title>wasup!</title></head>
<body>'''
foot='''</body></html>'''

print header+body+foot
Comments