close
Warning:
Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.
- Timestamp:
-
Jun 26, 2008, 7:15:04 PM (16 years ago)
- Author:
-
cmlenz
- Comment:
-
--
Legend:
- Unmodified
- Added
- Removed
- Modified
-
v3
|
v4
|
|
1 | 1 | = Form Processing = |
2 | 2 | |
3 | | TODO |
| 3 | Diva comes with a simple module supporting the processing of HTML forms. A [RequestHandlers request handler] that does some simple form processing typically looks something like this: |
| 4 | |
| 5 | {{{ |
| 6 | #!python |
| 7 | from diva.forms import Form, TextValidator |
| 8 | from diva.routing import redirect_to |
| 9 | from diva.templating import output, render |
| 10 | |
| 11 | class LinkForm(Form): |
| 12 | username = TextValidator(required=True) |
| 13 | url = TextValidator(required=True) |
| 14 | title = TextValidator(required=True) |
| 15 | |
| 16 | @output('submit.html') |
| 17 | def submit(request, response): |
| 18 | form = LinkForm() |
| 19 | if request.method == 'POST': |
| 20 | if 'cancel' in request.POST: |
| 21 | redirect_to('index') |
| 22 | if form.validate(request.POST): |
| 23 | # Form is valid, store data int the database |
| 24 | link = Link(**form.data) |
| 25 | link.store(app.db) |
| 26 | redirect_to('info', link.id) |
| 27 | return render(errors=form.errors) |
| 28 | }}} |
| 29 | |
| 30 | This simple example already does a couple of things you may not expect: |
| 31 | * When the form is redisplayed on `POST` due to validation errors, the form elements will already be populated with the previously entered values. |
| 32 | * The form submission is protected against Cross-Site Request Forgery (CSRF) attacks, by adding a form token both as a cookie, and as a hidden form input field. |
4 | 33 | |
5 | 34 | == API Documentation == |
6 | 35 | |
7 | 36 | [[PythonDoc(trunk, diva.forms)]] |
8 | | |