pythoncherrypycheetah

CherryPy can't seem to find CSS script (static or absolute paths)


i'm using the cherryPy framework to serve my site, but it cannot seem to find my css script with either the static path or the absolute path. The css script works fine if i just go to the index.tmpl file via the browser, but when i request it via cherrypy it does not use the css script.

root directory structure:

site.py
template/index.tmpl
static/css/main.css

site.py

import sys
import cherrypy
import os
from Cheetah.Template import Template

class Root:
    @cherrypy.expose
    def index(self):
        htmlTemplate = Template(file='templates/index.tmpl')
        htmlTemplate.css_scripts=['css/main.css']
        return str(htmlTemplate)



# On Startup
current_dir = os.path.dirname(os.path.abspath(__file__)) + os.path.sep

cherrypy.config.update({
    'environment': 'production',
    'log.screen': True,
    'server.socket_host': '127.0.0.1',
    'server.socket_port': 2000,
    'engine.autoreload_on': True,
    '/':{
        'tools.staticdir.root' : current_dir,
        },
    '/static':{
        'tools.staticdir.on' : True,
        'tools.staticdir.dir' : "static",
    },
})

cherrypy.quickstart(Root())

template/index.tmpl

<!DOCTYPE html>
<html>
<head>
    #for $script in $css_scripts:
        <link rel="stylesheet" href="$script" type="text/css" />
    #end for
        <link rel="stylesheet" href="C:/ABSOLUTE/PATH/main.css" type="text/css" />
</head>
<body>
        <! MY HTML CODE IS HERE>
</body>
</html>

what am i doing wrong?

EDIT
I have tried with static/css/main.css as the static path
I have also tried relative paths, relative to site.py and relative to index.tmpl

This is the error that i get:

GET http://localhost:2000/static/css/main.css 404 (Not Found)  

Solution

  • I'm not sure why this works, but after trying a million things this is what fixed it. If someone knows why then please enlighten me.

    here is the changed code:

    import sys
    import cherrypy
    import os
    from Cheetah.Template import Template
    
    class Root:
        @cherrypy.expose
        def index(self):
            htmlTemplate = Template(file='templates/index.tmpl')
            htmlTemplate.css_scripts=['static/css/main.css']
            return str(htmlTemplate)
    
    # On Startup
    current_dir = os.path.dirname(os.path.abspath(__file__)) + os.path.sep
    config = {
        'global': {
            'environment': 'production',
            'log.screen': True,
            'server.socket_host': '127.0.0.1',
            'server.socket_port': 2000,
            'engine.autoreload_on': True,
            'log.error_file': os.path.join(current_dir, 'errors.log'),
            'log.access_file': os.path.join(current_dir, 'access.log'),
        },
        '/':{
            'tools.staticdir.root' : current_dir,
        },
        '/static':{
            'tools.staticdir.on' : True,
            'tools.staticdir.dir' : 'static',
        },
    }
    cherrypy.quickstart(Root(), '/', config)