获取Flask以显示不在静态目录中的图像 [英] Get Flask to show image not located in the static directory

查看:191
本文介绍了获取Flask以显示不在静态目录中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试显示位于不是默认静态文件夹的文件夹中的图像.

I'm trying to display images located in a folder which is not the default static folder.

我的模板如下:

{% extends "layout.html" %}
{% block body %}
   Albums
   {% for album in albums %}
      <img src="{{ album ['ThumbPath'] }}">
   {% endfor %}
{% endblock %}

生成的HTML如下:

<!doctype html>
<title>iPhoto</title>
<link rel=stylesheet type=text/css href="/static/style.css">
<div class=page>
<h1>iPhoto</h1>  
<img src="/Users/Darrell/Pictures/iPhoto Library/Thumbnails/2013/08/09/20130809-180444/t264vvMaQM+GJosBP+4q+Q/_DSC1225.jpg">  
<img src="/Users/Darrell/Pictures/iPhoto Library/Thumbnails/2013/08/09/20130809-181030/urU3jqSKRgGNNP1MjKhpvg/_DSC1268.jpg">
<img src="/Users/Darrell/Pictures/iPhoto Library/Thumbnails/2013/08/09/20130809-181037/1zYyNYReRg+Sizx8v4BUkw/_DSC0923.jpg">  
<img src="/Users/Darrell/Pictures/iPhoto Library/Thumbnails/2013/08/09/20130809-181038/sKzEEB3jSf6GBs2heQIviA/Kelly.jpg">
</div>

如何获取要在生成的网页上呈现的图像. 图像的路径是正确的. 不能选择将所有图像复制到静态文件夹.

How can get the images to be rendered on the generated webpage. The path to the image is correct. It is no option to copy all images to the static folder.

达雷尔

推荐答案

好像您拥有文件系统路径而不是站点.例如,如果您使用站点http://test.com,则当您使用/启动资源时,路径将从站点根目录开始(站点的绝对路径(不带斜杠或带有./../的相对路径)是相对的). http://test.com//test将是http://test.com/test.您可以使用特定协议protocol://设置路径,或者为其他站点的当前站点//设置相同的路径.您总是可以在浏览器错误控制台中找到带有错误(完整)网址的错误.

Look like you have file system path instead site. When you start resource with / then path will start from site root (absolute path for site, without slash or with ./ or ../ is relative) for example if you have site http://test.com then / will be http://test.com/ or /test will be http://test.com/test. You can set path with specific protocol protocol:// or identical for current site // for another sites. You always can found errors with wrong (full) urls in your browser error console.

要访问文件系统,您必须使用file://协议,并且您的网址将类似于file:///Users/Darrell/Pictures/....但是它仅适用于您的计算机,并且可能存在安全问题.您还可以将此文件的符号链接设置为静态文件夹,这可以解决安全问题,但不能仅解决计算机的问题.为了完全解决此问题,可以更好地将此文件发布到您的应用程序或公共(专用)Web服务器上,以获取图像或其他静态文件.

To get access to file system you must use file:// protocol and you urls will be like file:///Users/Darrell/Pictures/.... But it will work only for your computer and can have security issues. You also can set symbolic link for this files to static folder and this resolve security issues, but not resolve issue with only your computer. To completely resolve this issue better publish this files to your application or on public (private) web servers for images or another static.

这篇关于获取Flask以显示不在静态目录中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆