2

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)  
Lex
  • 386
  • 1
  • 4
  • 20

2 Answers2

2

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.

  • I changed the config dictionary to have all the global variables under a sub-dictionary.
  • I got rid of the cherrypy.config.update() function and fed the config directly to cherrypy.quickstart()

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)
Lex
  • 386
  • 1
  • 4
  • 20
  • It wooooorks!!!! Thanks! I have no idea why adding global helps, but it does. I was going crazy with this after being stuck for two evenings. Now I wish I knew the reason... – Maciejg Feb 28 '17 at 21:30
1

Do not put absolute path to your CSS script, it should be relative.

Try setting it to href="/static/css/main.css" and the config to

[/static]
tools.staticdir.on = True
tools.staticdir.dir = 'static'
Jakub M.
  • 32,471
  • 48
  • 110
  • 179
  • That is what the config is set to, and regarding absolute path I have tried using the static path, and relative paths as well, relative to the site.py, and relative to index.html, but none of it works with cherrypy. – Lex May 18 '13 at 22:29
  • I remember I had some problems with setting the paths properly. Maybe try that: copy CSS to each directory around, and see if it works. If it works, delete one CSS and retry. – Jakub M. May 18 '13 at 22:41
  • thats a good idea, i hadn't thought of that, but unfortunately it still did not work. I'm thinking it has to be something in the cherrypy config because relative and absolute paths work when not using cherrypy, however i havent been able to figure out what needs to be changed. – Lex May 18 '13 at 22:48