我的Flask项目运行在一个临时目录下,项目的静态文件和模版都是放在其他地方的。因为Flask的模版目录和静态目录都是记录的相对路径,所以其实我只要修改项目的根路径。
我已经通过配置文件指定了APPLICATION_ROOT:
app.config.from_pyfile(CONFIG_FILE, silent=True)
print app.config.get('APPLICATION_ROOT')
R:\abc
print app.root_path
C:\Users\Administrator\AppData\Local\Temp
但当我访问http://127.0.0.1:5050/static/doc.html的时候Flask还是从C:\Users\Administrator\AppData\Local\Temp读取文件。
请问是我哪里没弄对?APPLICATION_ROOT是假的么?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
可以使用 Flask 的
template_folder和static_folder参数。app/app.py内容如下:/path/to/instance/templates/index.html内容如下:/path/to/instance/static/index.js内容如下:启动测试服:
在浏览器中访问:
另,
APPLICATION_ROOT指的是应用程序的根 URL,只和 session 的 cookie 设置有关,不是用于解决你遇到的问题的。参考:
Application Object
Builtin Configuration Values