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 6 (modified by anonymous, 13 years ago) (diff)


Simple Widget Form

Level: Beginner

Version 1.0 - Michael Schneider

created - 12/22/05 0.9 SVN version 356

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 the simplest validating widget that accepted a string and an integer


 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(widgets=[ W.TextField("name", default=None,
                                  W.TextField("age", default=0,

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 you 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>

   <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')" />