Bottle Static files

前端 未结 5 1925
小蘑菇
小蘑菇 2020-12-23 21:54

I have tried reading the docs for Bottle, however, I am still unsure about how static file serving works. I have an index.tpl file, and within it it has a css f

5条回答
  •  感情败类
    2020-12-23 22:21

    To serve static files using bottle you'll need to use the provided static_file function and add a few additional routes. The following routes direct the static file requests and ensure that only files with the correct file extension are accessed.

    from bottle import get, static_file
    
    # Static Routes
    @get("/static/css/")
    def css(filepath):
        return static_file(filepath, root="static/css")
    
    @get("/static/font/")
    def font(filepath):
        return static_file(filepath, root="static/font")
    
    @get("/static/img/")
    def img(filepath):
        return static_file(filepath, root="static/img")
    
    @get("/static/js/")
    def js(filepath):
        return static_file(filepath, root="static/js")
    

    Now in your html, you can reference a file like so:

    
    

    Directory layout:

    `--static
    |  `--css
    |  `--fonts
    |  `--img
    |  `--js
    

提交回复
热议问题