Download presentation
Presentation is loading. Please wait.
1
Python, CGI November 23, Unit 8
2
So Far We can write programs in Python (in theory at least) –Conditionals –Variables –While loops We can create a form for our web page –Adding controls Now we need some way to take that form information and process it somehow
3
CGI Common Gateway Interface It’s how we are going to pass information from our form to a web script In this case we’ll be taking our form information and giving it to our Python script To use CGI in Python we have to include the CGI module
4
Accessing Form Information First, we must import the CGI module –import cgi The next step is to load the form data into a variable in your Python script We’ll do this using the cgi.FieldStorage() function and assign it to some variable we create –Ex. form = cgi.FieldStorage()
5
What’s in the “form” Variable? The form variable will have all of the information stored in our HTML form Now we have to have a way to access the individual parts Each of our controls has a name attribute and a unique name –This is how we are going to access the values in each part of the form
6
Accessing the Information When we want the value entered into a specific control, we have to know the name we gave it in the HTML The method of accessing its value is: form[“name”].value –form is the name of the variable we assigned cgi.FieldStorage to (form = cgi.FieldStorage) –“name” is the value of name attribute in the HTML of the control –.value gives us the value for that control
7
Information Access, cont. If you specify a name that doesn’t exist in your form, you will get a Key Error when you press your submit button The square brackets in the form[“name”].value are used for accessing what’s called a dictionary We are looking up in the dictionary called form the entry for “name” –Each item in the dictionary will have a value associated with it
8
Using Values from a Form We can use the values from the form like we would any value returned by a function One thing to do is to assign it to a variable so that we can easily reuse it –customerName = form[“custName”].value –phoneNumber = int(form[“custPhone”].value All of the data stored in form will be a string –If we want a number, we have to convert it using the (int) or (float) functions
9
Calling the CGI Script The tag seems like it does nothing –But remember that it has to have the action attribute When the user presses the “submit” button on the form, the script specified in the action attribute is called and can access the information stored in our HTML form Remember that the action attribute specifies the URL of the script we want to call
10
Basics for a Form To add a form to our website requires two things: –The HTML form Some controls Submit Button –The Python script to do something with that information Why ask for the information if we don’t do something with it?
11
In-Class Example Creating a simple form Using a Python script to output a new web page
12
Questions? We’ll be covering this more in-depth on Friday when we have internet access and we can actually see a demo
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.