烧瓶不提供图像

时间:2016-02-16 07:08:58

标签: python flask sqlalchemy jinja2

我有一个用户上传文件的表单,其路径存储在数据库中,文件存储在'/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

1 个答案:

答案 0 :(得分:0)

想出来。我在蓝图中进行了手动设置,覆盖了默认的根静态文件夹。我把它们拿出来并默认返回默认的根静态文件夹,然后将我的模板代码更新为:

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

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

相关问题