views:

369

answers:

3

Hi All,

I'm having some trouble serving a static XML stylesheet to accompany some dynamically generated output from a CherryPy web app. Even my test case serving a static text file fails.

Static file blah.txt is in the /static directory in my application root directory.

In my main site file (conesearch.py contains the CherryPy ConeSearch page-handler class):

import conesearch
cherrypy.config.update('site.config')
cherrypy.tree.mount(conesearch.ConeSearch(), "/ucac3", 'ucac3.config')
...

And in site.config I have the following options:

[/]
tools.staticdir.root: conesearch.current_dir

[/static]
tools.staticdir.on: True
tools.staticdir.dir: 'static'

where current_dir = os.path.dirname(os.path.abspath(__file__)) in conesearch.py

However, my simple test page (taken straight from http://www.cherrypy.org/wiki/StaticContent) fails with a 404:

def test(self):
        return """
        <html> 
        <head>
        <title>CherryPy static tutorial</title>
        </head>
        <body>
        <a href="/static/blah.txt">Link</a>
        </body>
        </html>"""
test.exposed = True

It is trying to access 127.0.0.1:8080/static/blah.txt, which by my reckoning should be AOK. Any thoughts or suggestions?

Cheers,

Simon

A: 

I serve static files like this:

config = {'/static':
                {'tools.staticdir.on': True,
                 'tools.staticdir.dir': PATH_TO_STATIC_FILES,
                }
        }

cherrypy.tree.mount(MyApp(), '/', config=config)
Ryan Ginstrom
Nope, still no joy I'm afraid. Thanks though. Still getting a 404 on `blah.txt`. If I understand CherryPy config dicts/files correctly this should work exactly the same as the config files I have set up above right?
Simon Murphy
A: 

cherrypy.config.update should only receive a single-level dictionary (mostly server.* entries), but you're passing it a multi-level dictionary of settings that should really be per-app (and therefore passed to tree.mount).

Move those [/] and [/static] sections from your site.config file to your ucac3.config file, and it should work fine.

fumanchu
Oh, and change `<a href="/static/blah.txt">Link</a>` to `<a href="/ucac3/static/blah.txt">Link</a>`. ;)
fumanchu
Sorted. Works perfectly now, thanks.
Simon Murphy
A: 

I have a similar setup. Let's say that I want the root of my site to be at http://mysite.com/site and that the root of my site/app is at /path/to/www.

I have the following config settings in my server.cfg and am finding my static files without a problem:

[global]
...
app.mount_point = '/site'
tools.staticdir.root = '/path/to/www/'
[/static]
tools.staticdir.on = True
tools.staticdir.dir = 'static'

I'm serving up dojo files, etc, from within the static directory without a problem, as well as css. I'm also using genshi for templating, and using the cherrypy.url() call to ensure that my other URLs are properly set. That allows me to change app.mount_point and have my links update as well.

Tony Lenzi