Search code examples
pythoncssbottlestatic-files

Python bottle static file not applying CSS


So I am making a python bottle website and trying to apply CSS to a page with static file. But it is not applying even though it works for all other routes. The link in html is the same as all others and in inspect there is no error to see.

@get('/add/<filename:re:.*\.css>')
@get('/view/<filename:re:.*\.css>')
@get('/edit/<filename:re:.*\.css>')
@get('/<filename>')
def staticCSS(filename):
    return static_file(filename, root='views/css/')

This works for all routes except /edit/<username>/<title> I do not know what could cause this at all.


Solution

  • Seems to me you are over complicating the static file method. I find the easiest thing to do is this:

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

    This makes everything from the folder static accessible via a direct link. This is far easier to manage, since you can use any kind of nested sub folder structure under static that you want.