Page 334 -
P. 334

html form template




                              You were to turn the HTML form into a template within the yate.py module.

                           1  You were to start by creating a new template called templates/form.html that allows you
                              to parameterize the form’s CGI script name, method, input tags, and submit button text.

             The CGI script’s      <form action=$cgi_name method=$http_method>        The list of <INPUT>
             name and associated   Enter a timing value: $list_of_inputs<br />        tags and the submit
             HTTP method are                                                          button’s text is also
              parameterized.       <input type="Submit" value=$submit_text></form>    parameterized.


                   With the template ready, you were to write the code for two functions to add to yate.py.
               2
                   The first, called create_inputs(), takes a list of one of more strings and creates HTML <INPUT> tags
                   for each string, similar to the one that accepts TimeValue.
                   The second, called do_form(), uses the template from Part 1 of this exercise together with the create_
                   inputs() function to generate a HTML form:
                                                                                       This “continuation”
                             def create_inputs(inputs_list):
                                                                                        character lets you split
                                    html_inputs = ''                                    a long line of code over
                                                                                        multiple lines.
                T ake each name         for each_input in inputs_list:
                 and create an
                 <INPUT> tag.                html_inputs = html_inputs + ‘<input type= “Text" name="' +      \
                                                                        each_input + '" size=40>'



                                  return(html_inputs)

                             def do_form(name, the_inputs, method="POST", text="Submit"):


              Grab the              with open(‘templates/form.html') as formf:
              template from                 form_text = formf.read()
              your disk.
                                    inputs = create_inputs(the_inputs)

                 Create a           form = Template(form_text)             Create the list of <INPUT> tags.
                 template form.

                                 return(form.substitute(cgi_name=name, http_method=method,
                                            list_of_inputs=inputs, submit_text=text))




           298    Chapter 9
   329   330   331   332   333   334   335   336   337   338   339