我的 Flask 应用程序中未运行 js 脚本时遇到问题,发现根路径什么都没有。之后,我设置了一个简单的测试页面,如下所示:
@app.route('/test')
def index2():
return render_template('test.html')
Run Code Online (Sandbox Code Playgroud)
测试.html:
{{ request.script_root|tojson|safe }}
Run Code Online (Sandbox Code Playgroud)
它只是打印:
""
Run Code Online (Sandbox Code Playgroud)
怎么了?
不是script_root因为您直接在该服务器的根目录上为空吗?
来自Flask 文档:
你知道你的应用程序在哪里吗?如果您正在开发,答案很简单:它位于 localhost 端口上,并且直接位于该服务器的根目录上。
@app.route('/test')
def index2():
if not request.script_root:
# this assumes that the 'index' view function handles the path '/'
request.script_root = url_for('index', _external=True)
return render_template('test.html')
Run Code Online (Sandbox Code Playgroud)
测试.html:
{{ request.script_root|tojson|safe }}
Run Code Online (Sandbox Code Playgroud)
渲染:
"http://localhost:5000/"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3278 次 |
| 最近记录: |