/examples/web/forms.py
Python | 74 lines | 65 code | 4 blank | 5 comment | 0 complexity | 3a3c1cf2029116f424cb2385bad3c492 MD5 | raw file
1#!/usr/bin/env python 2 3"""Forms 4 5A simple example showing how to deal with data forms. 6""" 7 8from circuits.web import Server, Controller 9 10 11FORM = """ 12<html> 13 <head> 14 <title>Basic Form Handling</title> 15 </head> 16 <body> 17 <h1>Basic Form Handling</h1> 18 <p> 19 Example of using 20 <a href="http://circuitsframework.com/">circuits</a> and its 21 <b>Web Components</b> to build a simple web application that handles 22 some basic form data. 23 </p> 24 <form action="/save" method="POST"> 25 <table border="0" rules="none"> 26 <tr> 27 <td>First Name:</td> 28 <td><input type="text" name="firstName"></td> 29 </tr> 30 <tr> 31 <td>Last Name:</td> 32 <td><input type="text" name="lastName"></td> 33 </tr> 34 <tr> 35 <td colspan=2"> 36 <input type="submit" value="Save"> 37 </td> 38 </tr> 39 </table> 40 </form> 41 </body> 42</html>""" 43 44 45class Root(Controller): 46 47 def index(self): 48 """Request Handler 49 50 Our index request handler which simply returns a response containing 51 the contents of our form to display. 52 """ 53 54 return FORM 55 56 def save(self, firstName, lastName): 57 """Save Request Handler 58 59 Our /save request handler (which our form above points to). 60 This handler accepts the same arguments as the fields in the 61 form either as positional arguments or keyword arguments. 62 63 We will use the date to pretend we've saved the data and 64 tell the user what was saved. 65 """ 66 67 return "Data Saved. firstName={0:s} lastName={1:s}".format( 68 firstName, lastName 69 ) 70 71 72app = Server(("0.0.0.0", 8000)) 73Root().register(app) 74app.run()