调用循环html jinja无法调用表html上的某些图像

2024-06-25 23:13:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从controller/static/foto文件夹调用一些图像在table.html上显示

当我使用for loop在HTML上使用python函数img_dir调用value foto时

info/views.py

import controller import create app
import os

@info_blue.route('/img_dir')
def img_dir():
   app = create_app('dev')
   images = os.listdir('controller/static/foto')
   return render_template("table.html", foto=images)

table.html

{% for row in value %}
<tr>
   <td>{{row[0]}}</td>
   <td>{{row[1]}}</td>
   <td>{{row[2]}}</td>
   <td>{{row[3]}}</td>
   <td>
      {% for image in foto %}
         <img src="{{ url_for('static', filename='controller/static/foto/' + image) }}">
      {% endfor %}
    </td>
{% endfor %}
</tr>

它似乎不起作用,但当我试图用print(images)检查文件夹的内容时,我检查有3张照片

文件夹路径controller/static/foto中文件夹内的3张照片 1

为什么它还是空的

foto列上的表格似乎没有显示任何照片 2

注意:在foto列中,它只包含数据库中照片名称的varchar,仅用于指示照片显示时是否为正确的照片

这是文件夹路径 This is the folder path

试图从foto/调用单个映像

{% for row in value %}
<tr>
   <td>{{row[0]}}</td>
   <td>{{row[1]}}</td>
   <td>{{row[2]}}</td>
   <td>{{row[3]}}</td>
   <td>
      {% for image in foto %}
         <img src="{{ url_for('static', filename='controller/static/foto/' + image) }}">
      {% endfor %}
    <img src="{{ url_for('static', filename='foto/18_Aug_2021|02:33.jpg' + image) }}">
    </td>
{% endfor %}
</tr>

它可以工作,但只能在图像循环之外

在行循环内调用单个图像,但在图像循环外调用 4

当我尝试检查URL时,这是结果,就像没有调用图像一样

inspect the URL

{% for image in foto %} 
<img src="{{ url_for('static', filename='foto/'+image) }}"> 
{% endfor %} 
<img src="{{ url_for('static', filename='foto/18_Aug_2021|02:33.jpg' ) }}">

当我这样做时,即使是“+图像”也没有空间

second image inspect URL

直接复制到您的评论并获得jinja2.exception got Jinja2.exception

directly copy source code

使用path变量进行测试,无错误但不工作

you implement it like this?

if its work, there should be two picture each row

路径变量的新检查

just delete {{ row 4 }} those space must be from unimplement code


Tags: in图像imagesrc文件夹urlimgfor
2条回答

这是因为有一个名为table的函数,它像这样返回table.html,这就是为什么img_dir的返回函数不起作用的原因

the system taking priority of return function

解决方案是将它们作为一个id合并,因此返回将作为一个id合并,没有人拥有优先权

merge the code as one return

结果他们打电话来了,但又发生了另一个问题,我想可能要买新票了

problem solved but its just beginning

请检查您发送的文件路径是否正确。 我认为您已经在文件路径中放置了两次静态路径

<img src="{{ url_for('static', filename='controller/static/foto/' + image) }}">

此处文件名参数“controller/static/”不应存在,请将其删除并重试

我想它会起作用的

相关问题 更多 >