我的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
是假的么?
可以使用 Flask 的 template_folder
和 static_folder
参数。
app/app.py
内容如下:
from flask import Flask, render_template
app = Flask(__name__,
static_folder='/path/to/instance/static',
template_folder='/path/to/instance/templates')
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
/path/to/instance/templates/index.html
内容如下:
<script src="/static/index.js"></script>
/path/to/instance/static/index.js
内容如下:
document.write('Hello, world!');
启动测试服:
python app/app.js
在浏览器中访问:
另,APPLICATION_ROOT
指的是应用程序的根 URL,只和 session 的 cookie 设置有关,不是用于解决你遇到的问题的。
参考:
Application Object
Builtin Configuration Values