[Twisted-web] linking javascript and CSS file

arun chhetri uhcops at gmail.com
Thu Aug 7 12:42:01 EDT 2008


Hi,
I tried this example

class ColorPage(resource.Resource):
    def __init__(self, color):
        self.color =3D color

    def render(self, request):
        return """
        <html>
        <head>
          <title>Color: %s</title>
          <DEFANGED_link type=3D'text/css' href=3D'/css/styles.css'
rel=3D'Stylesheet' />
        </head>
        <body DEFANGED_STYLE=3D'background-color: #%s'>
          <h1>This is #%s.</h1>
          <p DEFANGED_STYLE=3D'background-color: white'>
          <a href=3D'/colors/'>Back</a>
          </p>
        </body>
        </html>
        """ % (self.color, self.color, self.color)

class ColorRoot(resource.Resource):
    def __init__(self):
        resource.Resource.__init__(self)
        self.requestedColors =3D []
        self.putChild('', ColorIndexPage(self.requestedColors))

    def render(self, request):
        # redirect /colors -> /colors/
        request.redirect(request.path + '/')
        return "Please use /colors/ instead."

    def getChild(self, path, request):
        if path not in self.requestedColors:
            self.requestedColors.append(path)
        return ColorPage(path)

class ColorIndexPage(resource.Resource):
    def __init__(self, requestedColorsList):
        resource.Resource.__init__(self)
        self.requestedColors =3D requestedColorsList

    def render(self, request):
        request.write("""
        <html>
        <head>
          <title>Colors</title>
          <DEFANGED_link type=3D'text/css' href=3D'/css/styles.css'
rel=3D'Stylesheet' />
        </head>
        <body>
        <h1>Colors</h1>
        To see a color, enter a url like
        <a href=3D'ff0000'>/colors/ff0000</a>. <br />
        Colors viewed so far:
        <ul>""")
        for color in self.requestedColors:
            request.write(
                "<li><a href=3D'%s' DEFANGED_STYLE=3D'color: #%s'>%s</a></l=
i>" % (
                color, color, color))
        request.write("""
        </ul>
        </body>
        </html>
        """)
        return ""

class HomePage(resource.Resource):
    def render(self, request):
        return """
        <html>
        <head>
          <title>Colors</title>
          <DEFANGED_link type=3D'text/css' href=3D'/css/styles.css'
rel=3D'Stylesheet' />
        </head>
        <body>
        <h1>Colors Demo</h1>
        What's here:
        <ul>
          <li><a href=3D'/colors'>Color viewer</a></li>
        </ul>
        </body>
        </html>
        """

if __name__ =3D=3D "__main__":
    from twisted.internet import reactor
    root =3D resource.Resource()
    root.putChild('', HomePage())
    root.putChild('colors', ColorRoot())
    styles =3D resource.Resource()
    styles.putChild('styles.css', static.File('styles.css'))
    root.putChild('css', styles)
    site =3D server.Site(root)
    reactor.listenTCP(8000, site)
    reactor.run()

with CSS code in external file styles.css, the code is like this

body {
    font-family: Georgia, Times, serif;
    font-size: 11pt;
}

h1 {
    margin: 10px 0;
    padding: 5px;
    background-color: black;
    color: white;
}

a{
    font-family: monospace;
}


p{
    padding: 10px;
}

But, there is no results.
Any pointer what I am doing wrong, I am using MAC OS leopard.

Thanks
dowell
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-web/attachments/20080807/0c=
f38676/attachment.htm


More information about the Twisted-web mailing list