Leif160519的blog Leif160519的blog

——————

目录
Flask在根目录'/'下返回静态HTML模版
/  

Flask在根目录'/'下返回静态HTML模版

本文摘自:https://blog.csdn.net/u014030117/article/details/46508901

实例化Flask类的时候做一个小设置static_url_path=''即可,把static_url_path设置为空字符串相当于设置把所有根目录下URL的访问都关联到/static/目录下,所以静态HTML模版中直接可以引用/js/something.js而不是/static/js/something.js这样麻烦

虽然他们实际上还是存放在/static/目录下,只是修改了映射关系

from flask import Flask

app = Flask(__name__, static_url_path='')

@app.route('/')
def index():
    return app.send_static_file('index.html')

if __name__ == '__main__':
    app.run()

“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill

标  题Flask在根目录'/'下返回静态HTML模版
作  者Leif160519
出  处https://github.icu/articles/2019/10/08/1570522669578.html
关于博主:坐标南京,运维工程师,如有问题探讨可以直接下方留言。
声援博主:如果您觉得文章对您有帮助,可以评论、订阅、收藏。您的鼓励是博主的最大动力!