[Twisted-Python] Web MVC hello world difficulties

Jonathan Lange jml at mumak.net
Tue Sep 3 22:12:39 MDT 2002


Hello,

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.

Thanks,
jml

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



# 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):
        wmvc.WModel.__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):
        self.model.updateGreeting()
    
    def factory_title(self, request, node):
        domhelpers.clearNode(node)
        return domwidgets.Text(self.model)
        
    def factory_greeting(self, request, node):
        domhelpers.clearNode(node)
        return domwidgets.Text(self.model)
        
    def factory_name(self, request, node):
        domhelpers.clearNode(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