2016-02-16 26 views
0

我有一个表格,用户上传文件,其路径存储在数据库中,文件存储在'/static/uploads/images/'烧瓶没有提供图片

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

picture_location是其中路径被保持的列。 asset_info是模型对象。

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

检查破碎图像图标的网址页面上显示它从数据库中检索的正确路径: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

哪里是你statuc文件夹? – realli

+0

@realli更新主目录的布局和静态文件夹信息。 – xGlorify

+0

@realli其实我找出了问题,我只是不知道如何解决它。因为这一切都发生在资产蓝图和'/ assets /'sub-route'(@ assets_blueprint.route('/ assets/')之内,所以出于某种原因追加了/ assets/'在图像路径的前面,像这样; '/ assets/static/uploads/images/motor.gif' 但是它从数据库检索的路径只是: '/ static/uploads/images/motor.gif' – xGlorify

回答

0

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

<img src="{{ url_for('static', filename=asset_info.picture_location) }}" />告诉它从静态根文件夹中检索路径。我还那么只好由字符串连接取出/static/追加我的数据库位置的格式,因为url_for('static' ....包括,当它生成的网址:

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