Speed up development with full-stack environments for every branch.

Learn More

TurboGears Forms Validation [Python]

Forked from Basic TurboGears Example.

412 Runs 413 Views 139 Copies

Shows how to display a basic form, validate data and handle validation errors with TurboGears2

Saved

Saved

amol 16

amol
published 4 years ago

from wsgiref.simple_server import make_server
from tg import TGController, AppConfig, expose, validate
from tw2.forms import TableForm, TextField
from tw2.core import DateValidator
from datetime import date

class BirthdayForm(TableForm):
    action = '/submit_birthday'
    birthday = TextField(label='Your Birthday', placeholder='2005-09-01',
                         validator=DateValidator(format='%Y-%m-%d',
                                                 required=True))


class RootController(TGController):
  @expose('index.html')
  def index(self, **kw):
    return dict(form=BirthdayForm)

  @expose('birthday.html')
  @validate(BirthdayForm, error_handler=index)
  def submit_birthday(self, birthday):
    today_is_birthday = (date.today() == birthday)
    return dict(birthday=birthday, today_is_birthday=today_is_birthday)
    
    
config = AppConfig(minimal=True, root_controller=RootController())
config.renderers = ['genshi']
config.use_toscawidgets2 = True

print "Serving on port 80..."
httpd = make_server('', 80, config.make_wsgi_app())
httpd.serve_forever()
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).