[Twisted-web] Multiple choices

Tommi Virtanen tv at tv.debian.net
Mon Jul 5 06:02:08 MDT 2004

Justin Johnson wrote:
> Here's an example of what I did to get a multiple choice select box. 
> It's been a while since I did this so it could probably use some
> cleanup.  But it is working for me.

Here's a cleaned up and modernized version, with an example.

Please explicitly say you are cool with this getting included
in nevow -- I'd be happy to file the issue.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: multichoice.py
Type: text/x-python
Size: 3141 bytes
Desc: not available
Url : http://pyramid.twistedmatrix.com/pipermail/twisted-web/attachments/20040705/91f35e1b/multichoice.py
-------------- next part --------------
# -*- python -*-
from twisted.application import service, internet
from nevow import compy, rend, inevow, appserver
from formless import webform, annotate, iformless
import multichoice

ANIMALS = ['dogs', 'cats', 'snakes']

class IAnimals(annotate.TypedInterface):
    def remember(self,
    remember = annotate.autocallable(remember)

class Animals(object):
    __implements__ = IAnimals
    like = None
    hate = None

    def remember(self, like, hate):
        self.like = like
        self.hate = hate
        print 'REMEMBER', self
        return self

    def __repr__(self):
        return '<%s like=%r hate=%r>' % (

class MyResource(rend.Page):
    docFactory = rend.htmlstr(
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

<html xmlns="http://www.w3.org/1999/xhtml"
    <link href="/form_css" rel="stylesheet" type="text/css" />
    <nevow:invisible nevow:render="input"/>
    child_form_css = webform.defaultCSS

    def configurable_(self, context):
            return context.locate(inevow.IHand)
        except KeyError:
            return Animals()

    def render_input(self, context, data):
        formDefaults = context.locate(iformless.IFormDefaults)
        methodDefaults = formDefaults.getAllDefaults('remember')
        conf = self.locateConfigurable(context, '')
        methodDefaults['like'] = conf.original.like
        methodDefaults['hate'] = conf.original.hate
        print 'RENDER', conf.original
        return webform.renderForms()

site = appserver.NevowSite(MyResource())
application = service.Application("multichoice-example")
webServer = internet.TCPServer(8080, site)

More information about the Twisted-web mailing list