[Twisted-Python] Web MVC hello world difficulties

Jonathan Lange jml at mumak.net
Wed Sep 4 00:12:39 EDT 2002


I am trying to use Web MVC to create a simple page that has a form with
a single text field. When something is entered into the text field,
'Hello World!' should be displayed in the greeting section.

I cannot get it to work, the greeting field remains unchanged, and the
value in the text field keeps being reset.

Below is a pretty simple example that demonstrates my problem.


<!-- index.xhtml -->
    <title class="title">Title will go here</title>
    <h1 class="title">
        Title will go here
    <p class="greeting">greeting</p>
    <form action="">
        <input type="TEXT" id="name"/>

# hello.rpy
import helloresource

model = helloresource.MHello()
resource = helloresource.CHello(model)

# helloresource.py
from twisted.web import wmvc
from twisted.web import domwidgets
from twisted.web import domhandlers
from twisted.web import domtemplate
from twisted.python import domhelpers
class MHello(wmvc.WModel):
    def __init__(self):
        self.title = "Mezzanine"
        self.name = "World"
        self.greeting = ""
    def updateGreeting(self):
        self.greeting = 'Hello, %s!' % self.name
class VHello(wmvc.WView):
    templateFile = 'index.xhtml'
    def setUp(self, request, document):
    def factory_title(self, request, node):
        return domwidgets.Text(self.model)
    def factory_greeting(self, request, node):
        return domwidgets.Text(self.model)
    def factory_name(self, request, node):
        return domwidgets.InputText(self.model)
class NameHandler(domhandlers.SingleValue):
    def check(self, request, data):
        if data:
            return 1

    def commit(self, request, node, data):
        domhandlers.SingleValue.commit(self, request, node, data)
        print "data: %s" % data

class CHello(wmvc.WController):
    def factory_name(self, model):
        return NameHandler(model)

wmvc.registerViewForModel(VHello, MHello)
wmvc.registerControllerForModel(CHello, MHello)

More information about the Twisted-Python mailing list