cs108 lecture 24 web forms
play

CS108 Lecture 24: Web Forms Processing CGI Data Aaron Stevens 27 - PDF document

CS108 Lecture 24: Web Forms Processing CGI Data Aaron Stevens 27 March 2009 1 Overview/Questions Review: Python and HTML HTML Forms Processing Form Fields Organizing a Python Web Application using functions, decision


  1. CS108 Lecture 24: Web Forms Processing CGI Data Aaron Stevens 27 March 2009 1 Overview/Questions – Review: Python and HTML – HTML Forms – Processing Form Fields – Organizing a Python Web Application using functions, decision making, etc. 2 1

  2. Review: Python and HTML Recall: HTML is the Hyper Text Markup language, used to describe the formatting of web pages. Python can generate HTML as output from any program. Run it on a web server, and you get a web page as output. 3 Hello World Python Web 4 2

  3. HTML Output This is the HTML Produced by the Python Script – As seen in Web Browser’s View Page Source. 5 HTML Forms HTML Forms are a way to provide input to the webserver: Forms are created with the <FORM> tag. 6 3

  4. HTML Forms Here is the code that created that form: HTML/GUI Display: 7 Processing CGI Form Fields Common Gateway Interface or CGI is a way for web applications to send data to the server. Python’s CGI module provides access to a dict of all form fields and their values. Use the cgi.FieldStorage() function to get this dict , which I am referring to as form . 8 4

  5. Processing CGI Form Fields Notice a few things: – Check that a field exists before ‘reading’ it. – Notice the item in the dict is an object of type cgi.MiniFieldStorage . The data we want is its .value attribute. 9 CGI Data in HTML Response After we ‘read’ the CGI Fields, we can use in our program/output: 10 5

  6. HTML Forms There are all kinds of HTML form elements: type=“radio” type=“checkbox” 11 HTML Forms There are all kinds of HTML form elements: <option>… </option> tag: 12 6

  7. More HTML Forms… Reference: http://www.w3schools.com/HTML/html_forms.asp for more syntax, etc. 13 Reading Other CGI Form Data Here are a variety of CGI Form Fields : 14 7

  8. Reading Other CGI Data All Form Fields are presented to Python as Type MiniFieldStorage. This code: Produces this HTML output: 15 Reading Other CGI Data These are key-value pairs: Notice that the checkboxes might produce a list of MiniFieldStorage objects. – Or a single MiniFieldStorage if only one checkbox was selected! 16 8

  9. Reading a CGI Data list If needed, check type of form fields… And process as a list of MiniFieldStorage Notice output obtained by indexing into this list: 17 Organizing a Python Web App Separate computation from presentation: – Write functions to do the computation, and unit test with a non-web, console application. – After you are satisfied with inputs/outputs for the functions, write the CGI/HTML parts and test. Always test computation first, and then begin work on presentation. 18 9

  10. Organizing a Python Web App Separate each web interaction into its own function. – Print a web form. – Display an output. – Got multiple paths/outputs -- one function each! 19 Take-Away Points – HTML <FORM> tag – CGI field data. – Be mindful of what type of data you are expecting. When in doubt, use introspection! 20 10

  11. Student To Dos – Readings:  HTML Tutorial: http://www.w3schools.com/HTML/ – Forms (Friday) – HW 10 due Tuesday 3/31 21 11

Recommend


More recommend