【问题标题】:Python - Flask: Static folder outside root directoryPython - Flask:根目录外的静态文件夹
【发布时间】:2019-05-26 23:55:57
【问题描述】:

只是为了好玩,我想了解如何使用PythonFlask 创建一个网站。该网站必须在我自己的计算机上运行,​​我将是唯一的客户。到目前为止,我已经完成了大部分我想做的事情,但现在我遇到了一个我无法解决的技术问题。

在客户端,我想显示服务器返回的图像。在my __init__.py 中,我放置了app = Flask(__name__, static_url_path='/static') 和我的html 文档<img src="/static/images/2012035.jpg" height="150px">。这就像一个魅力。

但实际上,我的图像位于应用程序目录之外的目录d:\genealogie\documenten 中,我不想将超过2000 个文件复制到目录static/images 中。

我试过了:

documenten = "d:\genealogie\documenten"
os.mkdir(documenten)

给出WinError 183,因为该目录已经存在。

我也试过了:

documenten = "d:\genealogie\documenten"
app = Flask(__name__, static_url_path='documenten')

给出ValueError:网址必须以斜杠开头。

我在这里看到了很多类似的问题,但不幸的是,我无法看到如何将答案用于我的具体问题。我能否以这样一种方式配置网站,使我作为用户可以要求说<img src="documenten/2012035.jpg" height="150px">,可能带有一些或其他localhost 前缀?非常感谢任何帮助。

编辑

我想要做的是让服务器访问服务器目录之外的目录。也许我可以通过在WAMP 中展示这可以多么容易地做到这一点来说明这一点。我们只需要在文件httpd.conf 中添加几行。例如:

Include "C:/wamp64/alias/*"

Alias /adressen "d:/adressen" 
<Directory "d:/adressen">
    Options Indexes FollowSymLinks Multiviews
    AllowOverride all
    Require all granted
</Directory>

Alias /genealogie "d:/genealogie" 
<Directory "d:/genealogie">
    Options Indexes FollowSymLinks Multiviews
    AllowOverride all
    Require all granted
</Directory>

服务器及其所有文件位于c:/wamp64 及其子目录中。但是,当我们在html 文档中包含&lt;img src="http://localhost/genealogie/documenten/doc1064.jpg"&gt;&lt;img src="http://localhost/adressen/doc5127.jpg"&gt; 时,这两个图像都可以很好地显示,尽管它们在物理上位于WAMP 之外,即使在不同的驱动器上也是如此。所以我的问题是:我们也可以使用FLASK 来做到这一点吗?

【问题讨论】:

  • 打开目录的想法很好,但 mkdir 会创建一个文件夹。只需使用 open 或其他东西。
  • 为什么不将 documenten 设为全局变量并按照您在烧瓶模板中的建议使用它。

标签: python flask wamp


【解决方案1】:

所以你需要用你的python代码打开文件夹,提取正确的图片并发送给你的用户?

你可以这样做

file = "d:\genealogie\documenten\your_file_name" #you may consider a path library so you can do this in windows or Linux 
os.open(file) #you should really use a image library here. open is just a placeholder

换句话说,您想在代码(函数、类等)中打开图像,然后对图像执行您需要执行的任何操作。例如返回给用户。

我个人会尝试使用这样的东西:

from flask import send_file 
file = "d:\genealogie\documenten\your_file_name" #you may consider a path library so you can do this in windows or Linux 
return send_file(file, mimetype='image/jpg') #in case of jpg image, you may opt to not use the mimetype

你不能这样做

documenten = "d:\genealogie\documenten" 
app = Flask(__name__, static_url_path='documenten')

但是为什么呢? 基本上static_url_path 是用户在浏览器中输入的网址。这与您在服务器上的文件夹结构无关。哎呀,您甚至不需要在服务器上拥有文件夹。

您的互联网呈现的结构不必与您的文件系统的结构相关。基本上这是两个完全不同的世界在这里碰撞。

URL 用于分层构建网络,主要处理组织结构(域、子域)。另一方面,文件服务器可以以多种方式构建。通常你想表示文件的性质和/或文件的年龄。

顺便说一句,mkdir 命令会创建文件夹,但您已经有了一些。

【讨论】:

  • 感谢您的回答。我玩了一点os.open(),但到目前为止没有成功。此外,我的问题是我想在服务器目录之外的 html 文档中打开图像。我编辑了我的问题以使其更清楚。
  • 为什么不能按原样交付图像。我修改我的答案。
  • 没问题。只需将问题标记为已回答。并可能反映 URL 与服务器文件系统。这不是一个简单的话题。
  • 谢谢,这是解决问题的关键。当我想使一个目录的所有文件,比如d:\genealogy\documents,在服务器计算机上可供客户端使用时,我选择一个名称,比如gendocs,然后在routes.py:@app.route('/gendocs/&lt;filename&gt;')def gendocs(filename):@中添加几行987654332@。现在在 html 文档中,我们可以使用 &lt;img src="/gendocs/doc4473.jpg" height="150px"&gt; 这样的结构。
猜你喜欢
  • 2014-06-06
  • 1970-01-01
  • 1970-01-01
  • 2013-02-09
  • 2013-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多