最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Static files not being served on Bottle in Python - Stack Overflow

programmeradmin0浏览0评论

I'm trying to set an application up which takes a template HTML file and modifies it live. It's working to an extent, but the images and CSS on the pages aren't being served, and there are HTTP 500 errors on the console when they are requested.

This is my directory structure

Server/
    assets/
        css/
            img/
            jquery.css
            kickstart.css
            zellner.css
        js/
            jquery.min.js
            kickstart.js
        style.css
        tb_404.png
        tbrun1.png
        tbservers.png
    403.html
    404.html
    500.html
    appid
    index.html
    maintenance.html
    server.log
    server.py

This is how I set up the routing in server.py:

@error(403)
def error403(error):
    return static_file("403.html")

@error(404)
def error404(error):
    return static_file("404.html")

@error(500)
def error500(error):
    return static_file("500.html")

@route('assets/<filepath:path>')
def server_static(filepath):
    return static_file(filepath, root='assets')

And in my html files, the files are linked like this:

<script type="text/javascript" src="assets/js/jquery.snippet.min.js"></script>

Could it be due to the statics being in subdirectories in assets/? Or have I completely misunderstood how to use static_file?

This is the type of error I get on the Python console:

[07/May/2012 10:51:05] "GET /tempus/23 HTTP/1.1" 200 4501 <h1>Critical error while processing request: /tempus/assets/js/jquery.snippet.min.js</h1>

I don't understand why it's routing to /tempus/assets/ ...

Any help? Thanks!

I'm trying to set an application up which takes a template HTML file and modifies it live. It's working to an extent, but the images and CSS on the pages aren't being served, and there are HTTP 500 errors on the console when they are requested.

This is my directory structure

Server/
    assets/
        css/
            img/
            jquery.css
            kickstart.css
            zellner.css
        js/
            jquery.min.js
            kickstart.js
        style.css
        tb_404.png
        tbrun1.png
        tbservers.png
    403.html
    404.html
    500.html
    appid
    index.html
    maintenance.html
    server.log
    server.py

This is how I set up the routing in server.py:

@error(403)
def error403(error):
    return static_file("403.html")

@error(404)
def error404(error):
    return static_file("404.html")

@error(500)
def error500(error):
    return static_file("500.html")

@route('assets/<filepath:path>')
def server_static(filepath):
    return static_file(filepath, root='assets')

And in my html files, the files are linked like this:

<script type="text/javascript" src="assets/js/jquery.snippet.min.js"></script>

Could it be due to the statics being in subdirectories in assets/? Or have I completely misunderstood how to use static_file?

This is the type of error I get on the Python console:

[07/May/2012 10:51:05] "GET /tempus/23 HTTP/1.1" 200 4501 <h1>Critical error while processing request: /tempus/assets/js/jquery.snippet.min.js</h1>

I don't understand why it's routing to /tempus/assets/ ...

Any help? Thanks!

Share Improve this question asked May 7, 2012 at 9:54 Callum BoothCallum Booth 3774 silver badges16 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 10

I also had problems with serving static files. Here is my solution:

@route('/static/:filename#.*#')
def send_static(filename):
    return static_file(filename, root='./static/')

and when you want to access a static file eg. a template file:

@route('/')
def index():
    output = template('static/index.tpl')
    return output

You have to put the complete path to the file in root= and it depends on where the program is running. Take a look at this: http://bottlepy.org/docs/dev/tutorial.html?highlight=static_file#tutorial-static-files

Your @route decorator is incorrect for serve_static.

It should read @route('/assets/')

发布评论

评论列表(0)

  1. 暂无评论