[Twisted-web] twisted web and css files

John Aherne johnaherne at rocs.co.uk
Mon Nov 26 07:39:26 EST 2012

I seem to have a problem processing css files with twisted.web using
twisted templates.

I get a message in my Firefox error console saying that it could not load
the css file since the type was text/html not text/css

I am running on windows 7 32bit
python 2.7.3
twisted 12.2.0
twisted templates

The template renders fine but without the styling.

I have hunted around for some ideas as to what the problem is but most seem
to be that people have not added the static file location to root.

In theory I do not have that problem since the message says it has found
the file but will not process it since it has the wrong type.

In my template file I add the link for the css file and give it the correct
file type. But I assume I have overlooked something since it refuses to
process the css.

My template is based on one of the demo templates and I have really just
added the css link

<html xmlns:t="http://twistedmatrix.com/ns/twisted.web.template/0.1">
Add Admin User
<link href="styles/jahstyle.css" rel="stylesheet" type="text/css" />
<div t:render="header" />
<li t:render="widgets">
<t:slot name="widgetName"/>
<div t:render="footer" />

The python that is processing this file is based on the samples I found
with some additions to add the staic file for  the css

from twisted.web.server import Site, NOT_DONE_YET
from twisted.web.resource import Resource
from twisted.internet import reactor
from twisted.web import server, static

from twisted.web.template import Element, renderer, XMLFile, flattenString
from twisted.python.filepath import FilePath

class WidgetsElement(Element):
    loader = XMLFile(FilePath('iteration-1.xml'))

    widgetData = ['gadget', 'contraption', 'gizmo', 'doohickey']

    def widgets(self, request, tag):
        for widget in self.widgetData:
            yield tag.clone().fillSlots(widgetName=widget)

    def header(self, request, tag):
        return tag('Header.')

    def footer(self, request, tag):
        return tag('Footer.')

def printResult(result):
    print result

class Root(Resource):
    isLeaf = True
    def render_GET(self, request):
        request.write("<!DOCTYPE html>\n")
        #flattenString(request, Hello()).addCallback(request.write)
        flattenString(None, WidgetsElement()).addCallback(request.write)
        return NOT_DONE_YET

root = Root()
root.putChild('styles', static.File("styles"))
site = Site(root)
reactor.listenTCP(8080, site)

So if anyone has some clues as to what I should look for next that would be
very welcome.


John Aherne

*John Aherne*
020 7223 7567
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-web/attachments/20121126/0148ca59/attachment.htm 

More information about the Twisted-web mailing list