/examples/web/forms.py

https://bitbucket.org/prologic/circuits/ · Python · 74 lines · 47 code · 9 blank · 18 comment · 0 complexity · 3a3c1cf2029116f424cb2385bad3c492 MD5 · raw file

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