Page 280 -
P. 280

yet another cgi script


           Create another CGI script


           Let’s take a moment to recall what is required from the generate_
           timing_data.py CGI script. Based on your hand-drawn sketch from             It’s probably a good
           earlier, your need to generate a new HTML page that contains the top three   idea to add a title
           times for the selected athlete:                                            to this page.
                       This looks like a
                       HTML heading for
                       the page, right?
                                                    Timing data for Sarah:                    Let’s include

                                                                                              the ahtlete’s
                                                                                             full name and
                                                            2.18                             DOB here.
                                                            2.21

                           This might be best                2.22
                           rendered as an
                           unordered HTML list.
                                                      Home          Select another athlete.





                                                        Two hyperlinks: one jumps to the home
                                                         page, whereas the other returns to the
                                                         previous selection page.


           But how do you know which athlete is selected?

           When you click on a radio-button and then press the Select button, a new
           web request is sent to the server. The web request identifies the CGI script
           to execute (in this case, that’s generate_timing_data.py), together
           with the form’s data. The web server arranges to send the form’s data to your
           CGI script as its input. Within your code, you can access the form data using
           Python’s cgi module, which is part of the standard library:

           Import the                                                Grab all of the form data
           “cgi” library.   import cgi                               and put it in a dictionary.
                           form_data = cgi.FieldStorage()

                           athlete_name = form_data['which_athlete'].value

                                        Access a named piece of data from the form’s data.
           244    Chapter 7
   275   276   277   278   279   280   281   282   283   284   285