所以这很尴尬.我有一个应用程序,我把它放在一起,Flask
现在它只是提供一个静态HTML页面,其中包含一些CSS和JS的链接.我无法在文档Flask
中找到返回静态文件的位置.是的,我可以使用,render_template
但我知道数据没有模板化.我曾经想过send_file
或者url_for
是对的,但是我无法让它们发挥作用.与此同时,我正在打开文件,阅读内容,并Response
使用适当的mimetype来装配:
import os.path
from flask import Flask, Response
app = Flask(__name__)
app.config.from_object(__name__)
def root_dir(): # pragma: no cover
return os.path.abspath(os.path.dirname(__file__))
def get_file(filename): # pragma: no cover
try:
src = os.path.join(root_dir(), filename)
# Figure out how flask returns static files
# Tried:
# - render_template
# - send_file
# This should not be so non-obvious
return open(src).read()
except IOError as exc:
return str(exc)
@app.route('/', methods=['GET'])
def metrics(): # pragma: …
Run Code Online (Sandbox Code Playgroud) 我正在使用flask设置本地服务器.我目前要做的就是使用index.html页面中的img标签显示图像.但我不断收到错误
GET http://localhost:5000/
ayrton_senna_movie_wallpaper_by_bashgfx-d4cm6x6.jpg 404 (NOT FOUND)
Run Code Online (Sandbox Code Playgroud)
烧瓶在哪里寻找文件?一点帮助都会很棒.我的HTML代码是
<html>
<head>
</head>
<body>
<h1>Hi Lionel Messi</h1>
<img src= "ayrton_senna_movie_wallpaper_by_bashgfx-d4cm6x6.jpg ">
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我的python代码是:
@app.route('/index', methods=['GET', 'POST'])
def lionel():
return app.send_static_file('index.html')
Run Code Online (Sandbox Code Playgroud) 我有一个引用这样的静态对象的html文件
<img src="img/snacks.png">
<link href="css/bluestrap.css" rel="stylesheet">
Run Code Online (Sandbox Code Playgroud)
因此浏览器尝试调用此通道并且烧瓶无法执行此操作
http://127.0.0.1:5000/img/snacks.png
Run Code Online (Sandbox Code Playgroud)
跨多个文件有很多这样的引用,因此无法更改引用.我如何从FLASK提供这些静态文件
我已将所有这些静态文件复制到'static'文件夹并尝试了这个
@app.route('/<path:filename>')
def send_file(filename):
return send_from_directory('/static', filename)
Run Code Online (Sandbox Code Playgroud)
但是这不起作用,有没有其他方法可以做到这一点?或者我做错了什么?