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

Learn More

TurboGears Forms, fill SingleSelect value [Python]

Forked from TurboGears Forms Validation.

37 Runs 113 Views 39 Copies

Shows a basic form with a SingleSelect and how to pre fill it with a selected value.



amol 16

published 3 years ago

from wsgiref.simple_server import make_server
from tg import TGController, AppConfig, expose, validate
from tw2.core import Deferred
from tw2.forms import Form, TableLayout, SingleSelectField
from datetime import date, timedelta

def get_birthday_options():
  today = date.today()
  return [(today - timedelta(days=2), 'two days ago'),
          (today - timedelta(days=1), 'yesterday'), 
          (today, 'today'), 
          (today + timedelta(days=1), 'tomorrow'),
          (today + timedelta(days=2), 'day after tomorrow')]

class BirthdayForm(Form):
    action = '/submit_birthday'

    class child(TableLayout):
      birthday = SingleSelectField(label='Your Birthday', prompt_text=None, options=Deferred(get_birthday_options))

class RootController(TGController):
  def index(self, **kw):
    today = date.today()
    return dict(form=BirthdayForm, form_values=dict(birthday=today))

  @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())
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).