【发布时间】:2020-05-02 07:13:52
【问题描述】:
我在使用烧瓶方面非常新,并且有一个基本问题:如何提供对使用烧瓶访问静态文件(例如图像)呈现的 HTML 的访问。这是我的玩具示例:
我想在网站上呈现 logo.svg。该项目的结构为
Project
|
+ -- static
|
+ -- logo.svg
+ -- templates
|
+ -- test.html
+ -- run_flask.py
test.html 如下所示
<!DOCTYPE>
<html>
<head>
<title>demo</title>
<style>
body {
font-family: helvetica neue, helvetica, liberation sans, arial, sans-serif;
font-size: 14px;
}
</style>
</head>
<body>
<img src="./static/logo.svg" id="logo">
</body>
</html>
我的 run_flask-py 脚本包含:
import flask
from flask import render_template, send_from_directory
from flask_cors import CORS
app = flask.Flask(__name__, static_url_path='')
CORS(app)
app.config["DEBUG"] = True
@app.route('/<string:page_name>/')
def render_static(page_name):
return render_template('%s.html' % page_name)
app.run(port=5001)
现在运行脚本时,控制台输出为:
在http://127.0.0.1:5001/ 上运行(按 CTRL+C 退出)
到目前为止一切顺利,但在 chrome 中,http://127.0.0.1:5001/test/ 看起来像:
我已经看过How to serve static files in Flask,因为问题听起来很相似。但我实际上对建议的 send_from_directory 如何在这里为我提供帮助感到完全困惑。
有人可以帮忙吗?
【问题讨论】:
-
<img src="{{ url_for('static', filename='logo.svg') }}" id="logo">工作? -
很遗憾没有