Python-Ref > Dynamic web pages in Python > Working with forms
 
 

<-^^->
Moduly
Knihovní funkce

Working with forms

How to process data from forms in a dynamic page.
Processing data from forms does not differ from what was said in Passing arguments to dynamic webpages. The only difference is that the user-supplied data are in this case passed from a form.
Depending on the "method" argument of the form the data from the form are passed either as GET or POST data. GET data are passed as part of the URL, POST data are encoded in the HTTP request. For simple and short data GET is often used, however for more complex data and file upload POST has to be used. However this is not our concern because we are completely shielded from this fact by mod_python which translates both types of data into function arguments.
The following example shows how an input into a form is transformed into an GET atribute, encoded into URL and used to make a request to our dynamic page. Our script than takes care of the input and produces output according to it.
Expand/Shrink
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-Type" content="text/html; charset=utf-8" />
    <title>Simple search form</title>
  </head>
  <body>
    <form action="dpages5-1.py" method="get" id="search-box">
      <div>Search for: <input type="text" name="search_text" /></div>
      <div><input id="go" type="submit" value="Search" /></div>
    </form>
  </body>
</html>
URL:
dpages5-1.py?search_text=ech
Zdroj: (dpages5-1.py)
  1   import MySQLdb
  2   from MySQLdb.cursors import DictCursor
  3   
  4   page_template = '''
  5   <html>
  6   <head>
  7   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  8   <title>%s</title>
  9   </head>
 10   <body>
 11   %s
 12   </body>
 13   </html>'''
 14   
 15   def index( search_text=""):
 16     title = "Country list"
 17     body = "<h1>Countries whose name contains '%s'</h1>" % search_text
 18     body += "<table>"
 19     connection = MySQLdb.connect( user="test", passwd="pass123", db="world", host="localhost", cursorclass=DictCursor)
 20     cursor1 = connection.cursor()
 21   
 22     cursor1.execute( "SELECT Name,Population FROM Country WHERE Name LIKE '%%%s%%' ORDER BY Name;" % search_text)
 23     for i in range( cursor1.rowcount):
 24       data = cursor1.fetchone()
 25       body += "<tr><td>%s</td><td>%d</td></tr>" % (data["Name"], data["Population"])
 26     body += "</table>"
 27   
 28     cursor1.close()
 29     connection.close()
 30   
 31     return page_template % (title, body)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Country list</title>
</head>
<body>
<h1>Countries whose name contains 'ech'</h1><table><tr><td>Czech Republic</td><td>10278100</td></tr><tr><td>Liechtenstein</td><td>32300</td></tr></table>
</body>
</html>
Doba běhu: 331.1 ms
The example below shows a very simple code that just visualizes all the arguments that were passed to it. It can be useful for debugging of forms, to see what they upload.
Expand/Shrink
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-Type" content="text/html; charset=utf-8" />
    <title>Simple search form</title>
  </head>
  <body>
    <form action="dpages5-2.py" method="get" id="search-box">
      <div>Search for: <input type="text" name="search_text" /></div>
      <div><input id="go" type="submit" value="Search" /></div>
    </form>
  </body>
</html>
URL:
dpages5-2.py?search_text=ech
Zdroj: (dpages5-2.py)
  1   page_template = '''
  2   <html>
  3   <head>
  4   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  5   <title>%s</title>
  6   </head>
  7   <body>
  8   %s
  9   </body>
 10   </html>'''
 11   
 12   def index( req, **kw):
 13     
 14     body = "<table><tr><td><b>Argument</b></td><td><b>Value</b></td></tr>"
 15     for k,v in kw.iteritems(): # kw is a dictionary
 16       body += "<tr><td>%s</td><td>%s</td></tr>" % (k,v)
 17     body += "</table>"
 18   
 19     return page_template % ("Form debugger", body)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Form debugger</title>
</head>
<body>
<table><tr><td><b>Argument</b></td><td><b>Value</b></td></tr><tr><td>search_text</td><td>ech</td></tr></table>
</body>
</html>
Doba běhu: 5.9 ms
The following example shows a more complicated form. It serves as a simple overview of usable HTML form parts, it does not necessarily have to have any meaning.
Please observe the enctype="multipart/form-data" attribute. This is vital for your script to be able to receive the content of a submitted file. Without it only the name of the file will be submitted.
Because we use file upload, it is not possible to use GET to submit the form and POST must be used. Because of this, the example does not show anything on this page. It is intended to be downloaded and tried manually.
Expand/Shrink
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-Type" content="text/html; charset=utf-8" />
    <title>Example upload form</title>
  </head>
  <body>
    <form action="dpages5-3.py" method="post" id="search-box" enctype="multipart/form-data">
      <div>File upload: <input type="file" name="uploaded_file" /></div>
      <hr />
      <div>What to do:</div>
      <table>
	<tr><td>Check wellformedness:</td><td><input type="checkbox" name="check_wellformed" checked="checked"/></td></tr>
	<tr><td>Check validity:</td><td><input type="checkbox" name="check_validity"/></td></tr>
      </table>
      <hr />
      <div>What program to use:</div>
      <div><input type="radio" name="program" value="xmllint" checked="checked"/> xmllint</div>
      <div><input type="radio" name="program" value="custom"/> custom program</div>
      <hr />
      <div>File format:</div>
      <div>
	<select name="format">
	  <option>XML</option>
	  <option>XHTML</option>
	  <option>SGML</option>
	</select>
      </div>
      <hr />
      <div><input id="go" type="submit" value="Upload" /></div>
    </form>
  </body>
</html>
Zdroj: (dpages5-3.py)
  1   page_template = '''
  2   <html>
  3   <head>
  4   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  5   <title>%s</title>
  6   </head>
  7   <body>
  8   %s
  9   </body>
 10   </html>'''
 11   
 12   from mod_python import util
 13   
 14   def index( req, **kw):
 15     
 16     body = "<table><tr><td><b>Argument</b></td><td><b>Value</b></td></tr>"
 17     for k,v in kw.iteritems(): # kw is a dictionary
 18       # files are special
 19       if isinstance( v, util.Field):
 20         body += "<tr><td>%s</td><td>%s</td><td>%d bytes</td></tr>" % (k,v.filename,len(v.value))
 21       else:
 22         body += "<tr><td>%s</td><td>%s</td></tr>" % (k,v)
 23     body += "</table>"
 24   
 25     return page_template % ("Form debugger", body)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Form debugger</title>
</head>
<body>
<table><tr><td><b>Argument</b></td><td><b>Value</b></td></tr></table>
</body>
</html>
Doba běhu: 6.0 ms