wiki:WidgetlessForm
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 2 (modified by anonymous, 13 years ago) (diff)

just added a title

Creating WidgetLess? forms

TurboGear? forms widgets are quickly being developed, but are presently (2005-12-19) in a state of flux. This is an example I'd wished I had when starting on TurboGears a couple days ago. Note that this is a completely functional forms example.

The first part is the root controller.py code:

import turbogears
from turbogears import controllers

SelectionList = (('', ''),
                 ('Primary', '1',),
                 ('Secondary', '2'),
                 ('Tertiary', '3'),)

class Root(controllers.RootController):
    @turbogears.expose(template="tgforms.templates.test_form")
    def index(self, **kw):
        user = kw.get('user', "Mortimer Snodgrass")
        gender = kw.get('gender', '')
        radio_choice = kw.get('radio_choice', '')
        notes = kw.get('notes', '')
        ordinal = kw.get('ordinal', '')
        box1 = kw.get('box1') == 'checked' or False
        box2 = kw.get('box2') == 'checked' or False
        box3 = kw.get('box3') == 'checked' or False
        return dict(myself=self,
                    user=user,
                    box1=box1,
                    box2=box2,
                    box3=box3,
                    gender_selection=gender,
                    selection_list=SelectionList,
                    ordinal_selection=ordinal,
                    radio_choice=radio_choice,
                    notes=notes)

    def testform_submit(self, **kw):
        # validate/process the data
        return self.index(**kw)
    testform_submit.exposed = True

The next portion is the test_form.kid template:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:py="http://purl.org/kid/ns#" py:extends="'master.kid'" xmlns:n="http://nevow.com/ns/nevow/0.1" lang="en" xml:lang="en">
  <body>
    <p>test_form.kid template</p>
    <p>
    <form action="testform_submit" method="post">
      <table>
      <tr>
        <td>Text field</td>
        <td><input name="user" type="text" py:attrs="value=user" /></td>
      </tr>
      <tr>
        <td>Check box</td>
        <td>
          <input type="checkbox" checked="${std.checker(box1)}" 
                name="box1" value="checked" /> box1
          <input type="checkbox" checked="${std.checker(box2)}" 
                name="box2" value="checked" /> box2
          <input type="checkbox" checked="${std.checker(box3)}" 
                name="box3" value="checked" /> box3
        </td>
      </tr>
      <tr>
        <td>Gender</td>
        <td>
          <select name="gender">
          <option py:for="label, val in (
              ('', ''),
              ('Male', 'M'),
              ('Female', 'F'),)"
              py:content="label"
              value="${val}"
              selected="${std.selector(gender_selection==val)}"
          />
          </select>
        </td>
      </tr>
      <tr>
        <td>Selection</td>
        <td>
          <select name="ordinal">
          <option py:for="label, val in selection_list"
              py:content="label"
              value="${val}"
              selected="${std.selector(ordinal_selection==val)}"
          />
          </select>
        </td>
      </tr>
      <tr>
        <td>Radio</td>
        <td>
          <input type="radio" py:for="label, val in (
              ('One', '1'),
              ('Two', '2'),
              ('Three', '3'),)"
              py:content="label"
              value="${val}"
              name="radio_choice"
              checked="${std.selector(radio_choice==val)}"
          />
        </td>
      </tr>
      <tr>
        <td>Text area</td>
        <td><textarea name="notes" py:content="notes" 
                rows="4" cols="40" />
        </td>
      </tr>
      <tr>
        <td></td>
        <td>
        <input type="submit" name="submit" value="Save" />
        <input type="submit" name="cancel" value="Cancel" />
        </td>
      </tr>
    </table>
      </form>
      </p>
  </body>
</html>

Note above that there are two examples of selection lists. The first, Gender, is embedded in the template. The second, Selection, obtains its list from controller.py.