2018-05-28 Forms!

posted May 28, 2018, 8:11 PM by Konstantinovich Samuel   [ updated May 29, 2018, 8:48 AM ]
More resources for web related python:

Forms are the widgets that you fill out /check off / select, and then submit to a website. 

Here is a sample website with forms:

CGI - Common Gateway Interface
We use CGI so that code (in various languages) can execute and communicate with web browsers.

You have used CGI to make a python program send data to web browsers!

Now we need the web browsers to send data back to the program. (You already print results, but now your results can react to the parameters)

Simple way to get and use form data:

See the following two SEPARATE files working together here: 

1. Create an HTML form on your website. This can be an HTML file or even a py 

2. Create a form handling python program (use the filename that matches the action of the form):


<!DOCTYPE html>

<h2>This is a form</h2>

<form method="GET" action="formhandler.py">

Name:<input type="text" name="yourName" size="20" value="Bob"><br>

Repeat:<input type="text" name="count" size="20" value="0">

Use 'Yo':<input type="checkbox" name="greeting" value="yo"><br>

Pick a poke-meal:
<select name="pokechoice" size="1">
  <option selected>Pika-Stew</option>

<input type="submit" name="button" value="I'm done.">
<input type="submit" name="button" value="I'm confusedx.">



print "Content-type: text/html\n"

#help you see errors
import cgitb

# We need these library modules to retrieve the user's answers
import cgi

def cgiFieldStorageToDict( fieldStorage ):
   """Get a plain dictionary, rather than the '.value' system used by the cgi module."""
   ans = {}
   for key in fieldStorage.keys():
     ans[ key ] = fieldStorage[ key ].value
   return ans

def main():
    # ask the library function to retrieve all answers and put them
    #   into a dictionary
    form = cgiFieldStorageToDict(cgi.FieldStorage())

    #print the form data!
    print form
    print "<br>"

    #lets say you want 2 pieces of information, the name, and the count (number of times to print the name)

    #pick some values that you want as default 
    #(we want to check for -1 later because we REQUIRE this value to be filled in.)
    count = -1
    #try to replace it using the form (if it exists!)
    if "count" in form:
        #the data must be integer formatted!
        count = int(form['count'])

    #pick default value (this won't cause an error if they forget to fill it out)
    name = "Joe"
    #try to replace it
    if "yourName" in form:
        name = form["yourName"]

    #after getting all the info you need, you can now
    #print the body of your html using the variables that
    #you initialized with the form elements.
    if count == -1: #we didn't get the required field
        print "<h2>error! Did not receive all required form data!</h2>"
	print "<h1> Main Site!</h1>"
        for i in range(count):
            print "Hi " + name + "!<br>"


head = '''<!DOCTYPE html>


print head


print ''' </body>