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:
-
Jul 1, 2008, 9:24:32 PM (16 years ago)
- Author:
-
cmlenz
- Comment:
-
--
Legend:
- Unmodified
- Added
- Removed
- Modified
-
v4
|
v5
|
|
11 | 11 | class LinkForm(Form): |
12 | 12 | username = TextValidator(required=True) |
13 | | url = TextValidator(required=True) |
| 13 | url = TextValidator(required=True, pattern=r'^https?://') |
14 | 14 | title = TextValidator(required=True) |
15 | 15 | |
… |
… |
|
19 | 19 | if request.method == 'POST': |
20 | 20 | if 'cancel' in request.POST: |
21 | | redirect_to('index') |
| 21 | redirect_to('home') |
22 | 22 | if form.validate(request.POST): |
23 | | # Form is valid, store data int the database |
| 23 | # Form is valid, store data into the database |
24 | 24 | link = Link(**form.data) |
25 | 25 | link.store(app.db) |
… |
… |
|
32 | 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. |
33 | 33 | |
| 34 | == HTML Forms in Templates == |
| 35 | |
| 36 | Diva does not generate HTML markup for your forms automatically. The `diva.forms` package is only concerned with the form data, not the rendering of individual form elements and how they assembled into the larger form. |
| 37 | |
| 38 | For the form defined above, a simple template might contain something like this: |
| 39 | |
| 40 | {{{ |
| 41 | #!genshi |
| 42 | <form action="" method="post"> |
| 43 | <p> |
| 44 | <label>Your name: <input type="text" name="username" /></label> |
| 45 | <span py:if="'username' in errors" class="error">${errors.username}</span> |
| 46 | </p><p> |
| 47 | <label>Link URL: <input type="text" name="url" /></label> |
| 48 | <span py:if="'url' in errors" class="error">${errors.url}</span> |
| 49 | </p><p> |
| 50 | <label>Title: <input type="text" name="title" /></label> |
| 51 | <span py:if="'title' in errors" class="error">${errors.title}</span> |
| 52 | </p> |
| 53 | <hr /> |
| 54 | <p> |
| 55 | <input type="submit" /> |
| 56 | </p> |
| 57 | </form> |
| 58 | }}} |
| 59 | |
| 60 | Note that while you don't need to manually take care of filling in the form values, you do have to explicitly add any error messages that need to be displayed after a failed validation. |
| 61 | |
34 | 62 | == API Documentation == |
35 | 63 | |