Python, CGI - PowerPoint PPT Presentation

About This Presentation
Title:

Python, CGI

Description:

Python, CGI. November 23, Unit 8. So Far. We can write programs in Python (in theory at least) ... do this using the cgi.FieldStorage() function and assign it ... – PowerPoint PPT presentation

Number of Views:97
Avg rating:3.0/5.0
Slides: 13
Provided by: SarahM100
Category:
Tags: cgi | cgi | programming | python

less

Transcript and Presenter's Notes

Title: Python, CGI


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
  • Its how we are going to pass information from
    our form to a web script
  • In this case well 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
  • Well do this using the cgi.FieldStorage()
    function and assign it to some variable we create
  • Ex. form cgi.FieldStorage()

5
Whats 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
  • formname.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 doesnt exist in your
    form, you will get a Key Error when you press
    your submit button
  • The square brackets in the formname.value are
    used for accessing whats 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 formcustName.value
  • phoneNumber int(formcustPhone.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 ltformgt 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 dont do
    something with it?

11
In-Class Example
  • Creating a simple form
  • Using a Python script to output a new web page

12
Questions?
  • Well be covering this more in-depth on Friday
    when we have internet access and we can actually
    see a demo
Write a Comment
User Comments (0)
About PowerShow.com