[Twisted-Python] twisted web, documentation

William Dode wilk-ml at flibuste.net
Fri Apr 25 14:46:51 EDT 2003

VAN DER HEIJDEN Maarten <mvanderheijden at fininfo.fr> writes:

> Hello,
> 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 :

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())

William Dodé - http://flibuste.net

More information about the Twisted-Python mailing list