[Twisted-Python] Re: twisted web, documentation

François Pinard pinard at iro.umontreal.ca
Tue Apr 29 14:51:11 EDT 2003

[William Dode]

> > Being new to twisted, I didn't find in the documentation an example (in
> > DOM Template, widgets, woven or resource templates ), for implementing a
> > form.  Someone has a link?

> this is an extended ReportRequest with form :

Thanks a lot, William.

Your example (quoted in full below, for reference) has been both useful and
instructive.  I have a question, though.  What is the purpose of `isLeaf'?
It does not seem to be called ever, when I run your example.

import twisted.web.resource
import cgi
from twisted.protocols import http
from twisted.web import resource,error
class ReportRequest(twisted.web.resource.Resource):
    """ ReportRequest with forms and cookies """
    def isLeaf(self):
        return true
    def render(self, request):
        user = request.getUser()
        password = request.getPassword()

        if not user:
            request.setResponseCode(401, 'Authentication needed')
            request.setHeader('WWW-authenticate', 'Basic realm="enter new user and password"')
            errpage = error.ErrorPage(http.UNAUTHORIZED,"Unauthorized","401 Authentication required")
            return errpage.render(request)

        session = request.getSession()

            if request.args["text"]!="":
                session.text = request.args["text"]
            pass # no text arg
        session_text = getattr(session,"text","init")        
        # create and increment cookie counter,
            counter = int(request.getCookie("counter"))
            counter += 1
            counter = 0
        cookies = request.received_cookies
        args = request.args
        path = request.path
        _, host, port = request.getHost()
        url = request.prePathURL()
        uri = request.uri
        secure = (request.isSecure() and "securely") or "insecurely"
        return ("""\
    <HEAD><TITLE>Welcome To Twisted Python Reporting</title></head>

    <BODY><H1>Welcome To Twisted Python Reporting</H1>
    <LI>User : %(user)s
    <LI>Password : %(password)s
    <LI>The path to me is %(path)s
    <LI>The host I'm on is %(host)s
    <LI>The port I'm on is %(port)s
    <LI>I was accessed %(secure)s
    <LI>A URL to me is %(url)s
    <LI>My URI to me is %(uri)s
    <LI>Session text : %(session_text)s
    <LI>Cookies (incremented in each reload): <pre>%(cookies)s</pre>
    <LI>Form args : <pre>%(args)s</pre>
    <h2>Testing args</h2>
    Test args : <a href='?a=1&a=2&a=3&b=4'>?a=1&amp;a=3&amp;a=3&amp;b=4</a><br>
    Test forms with method=post enctype='multipart/form-data'
    <form method=post enctype='multipart/form-data'>
    <tr><td> text : </td><td> <input name=text type=text> </td></tr>
    <tr><td> file : </td><td> <input name=file type=file> </td></tr>
    <tr><td> select (multiple) : </td><td>
    <select name=select multiple><option>one</option><option>two</option><option>three</option></select>
    <input type=submit></form>
</html>""" % vars())

François Pinard   http://www.iro.umontreal.ca/~pinard

More information about the Twisted-Python mailing list