Bottle web app not serving static css files

早过忘川 提交于 2019-12-03 15:23:32

Instead specify your static route like this

@route('/<filename:path>')
def send_static(filename):
    return static_file(filename, root='static/')

This will serve any file in your static directory though not just css.

To make it stylesheet specific

@get('/<filename:re:.*\.css>')
def stylesheets(filename):
    return static_file(filename, root='static/')

Note: for the latter option you could put stylesheets in their own directory 'static/css' or just 'css' and keep them separate from other static resources (scripts, images etc.) to do this just specify the root parameter to be that directory e.g. `root='static/css'.

There are 2 problems that I can see:

  • The route for the CSS files should begin with a slash, ie.

    @route('/<filename>.css')
    
  • Only the matching part of the pattern is passed to stylesheets() in the filename argument, e.g. instead of main.css, it will be main. Change the code to this:

    @route('/<filename>.css')
    def stylesheets(filename):
        return static_file('{}.css'.format(filename), root='static')
    

Alternatively... rename your main.css file to main.tpl, bookend with <style> and </style>, move it into the /views directory along with your other templates, then simply add to the beginning of your return line:

return (template ("main"), template ("list", terms=terms, defins=defins))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!