Python 2.7 使用flask和jinja遍历目录
我的目标是遍历静态文件中的一个目录,并“动态”显示除一个之外的所有图片。我有迭代工作,我只是不能让忍者的一面工作来显示我的照片 server.pyPython 2.7 使用flask和jinja遍历目录,python-2.7,flask,jinja2,Python 2.7,Flask,Jinja2,我的目标是遍历静态文件中的一个目录,并“动态”显示除一个之外的所有图片。我有迭代工作,我只是不能让忍者的一面工作来显示我的照片 server.py @app.route('/ninja') def ninja(): for filename in os.listdir('static/images'): if filename.endswith(".jpg") and filename != "notapril.jpg": allninjas =
@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>
{导航%中的项目的%1}
{%endfor%}
{listdir%中的文件名为%s}
{%if文件名%}
{%endif%}
{%endfor%}
底部标签将显示我的目录中最后一只忍者龟的图片。我不能让另一个忍者和如果循环工作。
请帮助我这周刚开始忍者。这里有几件事。让我们从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\u 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>
return render_template('ninja.html', navigation=navigation,
listdir=listdir,
allninjas=allninjas,
color=color)
然后将模板更改为如下所示:
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>
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)
我希望这能有所帮助。这里发生了几件事。让我们从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\u 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>
return render_template('ninja.html', navigation=navigation,
listdir=listdir,
allninjas=allninjas,
color=color)
然后将模板更改为如下所示:
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>
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)
我希望这能有所帮助。哇,我真的很感激你的解释。我之前曾尝试创建一个列表,但我没有将.append()添加到列表中,因此我猜没有添加任何内容。就像我说的,我是新来的。我非常感谢你花时间回答我的问题。太棒了。哇,我真的很欣赏你的解释。我之前曾尝试创建一个列表,但我没有将.append()添加到列表中,因此我猜没有添加任何内容。就像我说的,我是新来的。我非常感谢你花时间回答我的问题。太棒了。