Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Version 12 (modified by diwaker, 11 years ago) (diff)


Simple Widget Form

Level: Beginner

Version 1.0 - Michael Schneider

created - 12/22/05 0.9 SVN version 356 Updated 02/13/2006 by Diwaker Gupta

Widgets are in a state of flux. This is a journal of creating my first widget form in turbogears.

Please feel free to edit this page to improve the code, or add user instructions.


Create a simple application that demonstrates a form widget.


 http://localhost:8080/ - the main form page

  • time created
  • form with two elements and a submit button
    • name field - text string
    • age field - integer string

 http://localhost:8080/processForm - page form is submitted to for processing

 http://localhost:8080/youDidIt - very simple page to indicate you are at the end of the journey

project setup

create a new project with tg-admin

tg-admin quickstart
.... mywidgets


import time
import cherrypy
import turbogears
import turbogears.widgets as W
import turbogears.validators as V
from turbogears import controllers

# I created my form widget here
my_form = W.TableForm([ W.TextField("name", default=None, validator=V.String()),
                        W.TextField("age", default=0, validator=V.Int())])

class Root(controllers.RootController):

   @turbogears.expose( template="mywidgets.templates.myFormPage")
   def index( self, name='', age='', submit=''):
      return dict(message="", now=time.ctime(), my_form= my_form)

   @turbogears.expose( template="mywidgets.templates.myFormPage",
                       inputform= my_form)
   def processForm( self, name='', age='', submit=''):
       # Check for validation errors
       if cherrypy.request.form_errors:
           return dict(message="", now=time.ctime(), my_form= my_form)

       # At this point, you have valid form data.
       # you would normally stash form info in
       # your model here
       raise cherrypy.HTTPRedirect(turbogears.url("/youDidIt"))

   def youDidIt(self):
       return " Your form had valid data, good job!!!!!!"

Create your Kid Template

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns=""

   <meta content="text/html; charset=UTF-8" http-equiv="content-type"
   <title>Welcome to TurboGears</title>

   <!-- grab the value of now from the dictionary returned by the
        controller method that called this template -->
   <p>Form now <span py:replace="now">now</span>.</p>

   <h2>Let's start with a Simple Form</h2>

   <!--  The next line inserts the instance of our form in
         my_form into the kid template.

         when submit is pressed, the form will be submitted to
         /processForm -->

   <span py:replace="my_form.insert(action='processForm')" />