使用Flask无法正确显示图像

时间:2019-08-19 09:17:53

标签: python html flask flask-admin

我正在设置一个需要显示一些图像的应用程序。我的图片位于应用程序文件夹之外,这就是为什么我使用了此图片:

    @app.route('/<path:filename>')
    def download_file(filename):
          return send_from_directory(MEDIA_FOLDER, filename=filename, as_attachment=True)

我的文件夹的体系结构是:

--images1
  |__ image1.png
  |__ image2.png
--images2
  |__ image3.png
  |__ image4.png
--web
  |__ app.py
  |__ templates
    |__ index.html

我将所有图像路径存储在一个列表中,如下所示:

images_path = ['image1.png', 'image2.png', ...]

此刻,我试图将图像显示为:

{% for image in images_path %}

<img src="{{ url_for('download_file', filename="{{ image }}") }}">

使用它可以显示始终相同的图像。在我的示例中,image1.png。 我尝试检查for循环是否有效,并且有效。

我已经尝试过了:

{{ image }} CHECKER
<img src="{{ url_for('download_file', filename="{{ image }}") }}">

,它显示在我的页面上:

>>> 

image1.png CHECKER then the `image1.png`

image2.png CHECKER but also `image1.png`

更新1

在我的app.py中,我添加了以下内容:

@app.route('/<path:filename>')
def download_file(filename):
images_folder = [images1, images2]
try:
    for image_folder in imagesfolder:
        MEDIA_FOLDER = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) + str(image_folder) + "\\"
            return send_from_directory(MEDIA_FOLDER, filename=filename, as_attachment=True)
        except FileNotFoundError:
            abort(404)

在我的template.html上,添加了以下内容:

{% for image in images_path %}

<img src="{{ url_for('download_file', filename=image) }}">

如何在此处正确设置路径? 如何链接图像及其目录?


更新2

我通过以下方式更改了脚本:

    @app.route('/<path:filename>')
    def download_file(filename):
        try:
            return send_file(filename, as_attachment=True)
        except FileNotFoundError:
            abort(404)

文件名将定义为:

>>>> filename = str(os.getcwd()) + str(list_machine[i]) + str(elem)

当我检查页面上的元素时,URL似乎正确,但是未显示图像。


如何显示文件夹中存储的所有图像?

谢谢。

2 个答案:

答案 0 :(得分:1)

问题并非来自{{image}},但在html img之后,您的代码{%endfor%}中没有看到。

所以正确的代码是:

{% for image in images_path %}
<img src="{{ url_for('download_file', filename="{{ image }}") }}">
{% endfor %}

更新1:

我认为您在代码中犯了一些错误:

@app.route('/<path:filename>')
def download_file(filename):
    images_folder = ['images1', 'images2']
    MEDIA_FOLDER = []
    try:
        for image_folder in imagesfolder:
            MEDIA_FOLDER.append(os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) + str(image_folder) + "\\")
                return send_from_directory(MEDIA_FOLDER, filename=filename, as_attachment=True)
    except FileNotFoundError:
        abort(404)

答案 1 :(得分:1)

您可以尝试以下方法:

  var client = new RestClient("https://dastanito.ir/test/ex2/api/storiesmaster/read.php");

            var request = new RestRequest("");

            var response = client.Post(request);
            MessageBox.Show(response.Content.ToString());

,其中<img src="{{ url_for('download_file', filename=image) }}"> 变量定义为:image

您必须通过列出文件来定义此变量,您可以使用:image = str(os.getcwd()) + '/path/to/image'

您名为os.listdir('path/directory')的函数应为:

download_file

别忘了导入@app.route('/<path:filename>') def download_file(filename): try: filename=os.path.join(app.root_path, filename) return send_file(filename) except FileNotFoundError: abort(404) 函数。

对我有用。希望能帮助到你。