[Twisted-Python] twisted web, documentation

William Dode wilk-ml at flibuste.net
Fri Apr 25 12:46:51 MDT 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()

        try:
            if request.args["text"]!="":
                session.text = request.args["text"]
                session.touch()
        except:
            pass # no text arg
        session_text = getattr(session,"text","init")        
        # create and increment cookie counter,
        try:
            counter = int(request.getCookie("counter"))
            counter += 1
        except:
            counter = 0
        request.addCookie("counter",counter)
            
        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 ("""\
<HTML>
    <HEAD><TITLE>Welcome To Twisted Python Reporting</title></head>

    <BODY><H1>Welcome To Twisted Python Reporting</H1>
    <UL>
    <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>
    </UL>
    <hr>
    <h2>Testing args</h2>
    Test args : <a href='?a=1&a=2&a=3&b=4'>?a=1&a=3&a=3&b=4</a><br>
    Test forms with method=post enctype='multipart/form-data'
    <form method=post enctype='multipart/form-data'>
    <table>
    <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>
    </td></tr></table>
    <input type=submit></form>
    </body>
</html>""" % vars())




-- 
William Dodé - http://flibuste.net




More information about the Twisted-Python mailing list