Flask 未提供图片

Flask not serving image

我有一个用户上传文件的表单,它的路径存储在数据库中,文件存储在 '/static/uploads/images/'

现在在我的模板中,当我要求显示该图像时,我正在这样做:<img src="{{ asset_info.picture_location }}" />

picture_location 是保留路径的列。 asset_info 是模型对象。

由于某种原因,它在尝试检索图像时返回 404: GET /static/uploads/images/motor.gif HTTP/1.1" 404 -即使那是正确的路径而且我可以在其中看到该文件。

检查页面上损坏的图像图标的 url 表明它正在从数据库中检索正确的路径:http://localhost:5000/static/uploads/images/motor.gif

客户端浏览器的响应是这样的:

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

我在谷歌上搜索了很多,但没有任何运气。是否有我应该调用/构建的单独方法来提供图像?

此蓝图信息:assets_blueprint = Blueprint('assets', __name__, static_folder='/static', template_folder='templates')

目录是:

app/
    assets/
        views.py
        asset_models.py
        asset_forms.py

    other_blueprints/
        etc.py

    static/
        uploads/
            images/
                motor.gif

app.py
config.py

想通了。我在蓝图中进行了手动设置,覆盖了默认的根静态文件夹。我把它们拿出来,它默认恢复为从默认根静态文件夹提供服务,然后将我的模板代码更新为:

<img src="{{ url_for('static', filename=asset_info.picture_location) }}" /> 告诉它从静态根文件夹中检索路径。然后我还必须通过从字符串连接中删除 /static/ 来附加我的数据库位置格式,因为 url_for('static' .... 在生成 url:

时包含它

旧: filename_input = str('static/uploads/images/' + str(request.files['photo'].filename)) 新的: filename_input = str('uploads/images/' + str(request.files['photo'].filename))