使用 flask 和 jinja 遍历目录
Iterating through directory with flask and jinja
我的目标是遍历我的静态文件中的目录并显示除一张 "dynamically" 之外的所有图片。我有迭代工作我只是无法让忍者的一面工作来显示我的照片。
server.py
@app.route('/ninja')
def ninja():
for filename in os.listdir('static/images'):
if filename.endswith(".jpg") and filename != "notapril.jpg":
allninjas = (os.path.join('static/images', filename))
else:
continue
return render_template('ninja.html', color=allninjas)
ninja.html:
<body>
{% for item in navigation %}
<img src="{{ allninjas }}"/>
{% endfor %}
{% for filename in listdir %}
{% if filename %}
<img src="{{ allninjas }}"/>
{% endif %}
{% endfor %}
<img src="{{ color }}"/>
</body>
底部标签将显示我目录中最后一张忍者神龟的图片。我无法让其他忍者 for 和 if 循环工作。
请帮助我这周才开始忍者。
这里发生了几件事。让我们从您的 Python 代码开始。正如@davidism 提到的那样,您需要创建一个列表,然后将该列表传递给您的模板。像这样...
@app.route('/ninja')
def ninja():
allninjas = []
for filename in os.listdir('static/images'):
if filename.endswith(".jpg") and filename != "notapril.jpg":
allninjas.append(os.path.join('static/images', filename))
else:
continue
return render_template('ninja.html', color=allninjas)
现在,您的模板已将某些内容分配给其 color
变量,这是一个在您的 Python 代码中称为 allninjas
的列表。这也是您的循环不起作用的原因,因为您没有为这些变量分配任何内容,只有 color
.
您可能想要做的是将您对 render_template
的调用更改为如下所示:
return render_template('ninja.html', allninjas=allninjas)
然后将模板更改为如下所示:
<body>
{% for filename in allninjas %}
{% if filename %} # not sure you really need this line either
<img src="{{ filename }}"/>
{% endif %}
{% endfor %}
</body>
我删除了很多。我不确定你对其他部分做了什么,但我会告诉你为什么我把它们拿出来。首先,你有两个循环打印 img
标签,图像源设置为 allninjas
,这将只打印每个图像的两个,除了你的循环变量在每种情况下都是未定义的。 navigation
和 listdir
不会从您的 Python 代码发送到模板,因此模板不知道它们是什么,也无法遍历它们。
您的代码定义了 color
,但没有定义其他内容,因此能够显示一张图片。我不确定您对所有这些其他变量的真正意图,所以除非您进一步解释,否则我无法帮助您。
如果您要定义所有这些变量,您的模板调用将如下所示:
return render_template('ninja.html', navigation=navigation,
listdir=listdir,
allninjas=allninjas,
color=color)
在每种情况下,例如 color=color
,第一部分 color=
指的是模板中的变量。您是在告诉模板应该分配给该变量的内容。第二部分,在本例中 color
是您要发送到模板的 Python 代码中的变量。所以:
return render_template('templatename.html', template_variable=Python_variable)
希望对您有所帮助。
我的目标是遍历我的静态文件中的目录并显示除一张 "dynamically" 之外的所有图片。我有迭代工作我只是无法让忍者的一面工作来显示我的照片。
server.py
@app.route('/ninja')
def ninja():
for filename in os.listdir('static/images'):
if filename.endswith(".jpg") and filename != "notapril.jpg":
allninjas = (os.path.join('static/images', filename))
else:
continue
return render_template('ninja.html', color=allninjas)
ninja.html:
<body>
{% for item in navigation %}
<img src="{{ allninjas }}"/>
{% endfor %}
{% for filename in listdir %}
{% if filename %}
<img src="{{ allninjas }}"/>
{% endif %}
{% endfor %}
<img src="{{ color }}"/>
</body>
底部标签将显示我目录中最后一张忍者神龟的图片。我无法让其他忍者 for 和 if 循环工作。 请帮助我这周才开始忍者。
这里发生了几件事。让我们从您的 Python 代码开始。正如@davidism 提到的那样,您需要创建一个列表,然后将该列表传递给您的模板。像这样...
@app.route('/ninja')
def ninja():
allninjas = []
for filename in os.listdir('static/images'):
if filename.endswith(".jpg") and filename != "notapril.jpg":
allninjas.append(os.path.join('static/images', filename))
else:
continue
return render_template('ninja.html', color=allninjas)
现在,您的模板已将某些内容分配给其 color
变量,这是一个在您的 Python 代码中称为 allninjas
的列表。这也是您的循环不起作用的原因,因为您没有为这些变量分配任何内容,只有 color
.
您可能想要做的是将您对 render_template
的调用更改为如下所示:
return render_template('ninja.html', allninjas=allninjas)
然后将模板更改为如下所示:
<body>
{% for filename in allninjas %}
{% if filename %} # not sure you really need this line either
<img src="{{ filename }}"/>
{% endif %}
{% endfor %}
</body>
我删除了很多。我不确定你对其他部分做了什么,但我会告诉你为什么我把它们拿出来。首先,你有两个循环打印 img
标签,图像源设置为 allninjas
,这将只打印每个图像的两个,除了你的循环变量在每种情况下都是未定义的。 navigation
和 listdir
不会从您的 Python 代码发送到模板,因此模板不知道它们是什么,也无法遍历它们。
您的代码定义了 color
,但没有定义其他内容,因此能够显示一张图片。我不确定您对所有这些其他变量的真正意图,所以除非您进一步解释,否则我无法帮助您。
如果您要定义所有这些变量,您的模板调用将如下所示:
return render_template('ninja.html', navigation=navigation,
listdir=listdir,
allninjas=allninjas,
color=color)
在每种情况下,例如 color=color
,第一部分 color=
指的是模板中的变量。您是在告诉模板应该分配给该变量的内容。第二部分,在本例中 color
是您要发送到模板的 Python 代码中的变量。所以:
return render_template('templatename.html', template_variable=Python_variable)
希望对您有所帮助。