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.

Changes between Version 3 and Version 4 of FormProcessing


Ignore:
Timestamp:
Jun 26, 2008, 7:15:04 PM (13 years ago)
Author:
cmlenz
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • FormProcessing

    v3 v4  
    11= Form Processing =
    22
    3 TODO
     3Diva 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
     7from diva.forms import Form, TextValidator
     8from diva.routing import redirect_to
     9from diva.templating import output, render
     10
     11class LinkForm(Form):
     12    username = TextValidator(required=True)
     13    url = TextValidator(required=True)
     14    title = TextValidator(required=True)
     15
     16@output('submit.html')
     17def 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
     30This 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.
    433
    534== API Documentation ==
    635
    736[[PythonDoc(trunk, diva.forms)]]
    8