【问题标题】:How to serve index.html by default via bottle?默认情况下如何通过瓶子服务 index.html?
【发布时间】:2015-11-26 01:52:34
【问题描述】:

我有一个静态文件的路由定义为

@bottle.route('/status/<filename>')
def server_static(filename):
    root = os.path.join(os.path.dirname(__file__), 'status', 'public_html')
    return bottle.static_file(filename, root=root)

当我调用http://localhost/status/index.html(或任何其他文件)时,它工作正常。

有没有办法在调用http://localhost/status时默认服务index.html这相当于Apache中的DirectoryIndex或nginx中的index

【问题讨论】:

    标签: python bottle


    【解决方案1】:

    添加/status规则并将filename的默认值设置为index.html

    @bottle.route('/status')
    @bottle.route('/status/<filename>')
    def server_static(filename='index.html'):
        root = os.path.join(os.path.dirname(__file__), 'status', 'public_html')
        return bottle.static_file(filename, root=root)
    

    【讨论】:

    • 注意:我没有设法让它与@bottle.route('/status')一起工作,我不得不附加一个斜线:@bottle.route('/status/')
    • @WoJ Bottle 不会自动去除尾部斜杠,因此 /status 和 /status/ 是不同的路线。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    • 1970-01-01
    • 2012-02-29
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多