在准备图像元数据以传输到模板时,我面临着我的变量 CollectionIPTCa 的问题:
return render_template("/Album2024a/Album2024b.htm", testarray = testarray1, **collectionIPTC = CollectionIPTCa**, CollectionIPTCmimic = "CollectionIPTCmimic", myi = i)
它的行为就像被清空:回到[]的初始状态; Jinja2 只收到空的 []。
我认为我的结构化代码中有错误
for p in ...
if ...
if ...
else ....
else ....
或者某处出现错误
这是我的flask_app.py 代码的摘录:
@app.route('/ImagesCabWillA/')
def ImagesCabWillA():
CollectionIPTCa=[]
testarray1=[12,24]
i=0
for p in Path('static/ImagesCabWillA').glob("*.*"):
if p.is_file() and p.suffix.lower() in (".jpg", ".jpeg"):
CollectionIPTCa.append(["item " + str(i)])
CollectionIPTCa[i].append(os.path.basename(p))
info = iptcinfo3.IPTCInfo(p, force=True)
if isinstance(info['object name'],bytes):
CollectionIPTCa[i].append(info["object name"].decode("utf-8"))
CollectionIPTCa[i].append(info["headline"].decode("utf-8"))
else:
print("info['object name'] is not byte type\n")
continue
i+=1
else:
print(os.path.basename(p) , " is not a jpg file\n")
continue
print("CollectionIPTCa :")
print(CollectionIPTCa)
# CollectionIPTCa=['truc', 'much', ['trac', 'mach']] # this test was working ok)
return render_template("/Album2024a/Album2024b.htm", testarray = testarray1, collectionIPTC = CollectionIPTCa, CollectionIPTCmimic = "CollectionIPTCmimic", myi = i) # "i" doesn't work, i is ok (integer)
从那里,您可以看到我对更简单的变量进行的测试以通过返回指令。
在控制台模式下(不是 Flask 提供“路由”,因此...),CollectionIPTCa 的值是(如预期:-)):
[['item 0', '09_DSCF8528.JPG', 'Les débuts', 'Le living , WiFi'], ['item 1', '07_IMG-20220325-WA0021.jpeg', 'Le nouveau poêle', '~~'], ['item 2', '05_IMG-20220325-WA0014.jpeg', 'Vers la rrue Copinette', 'Centre Willerzie'], ['item 3', '06_IMG-20220517-WA001
3.jpeg', "L'Hotel de la Poste", '"LE" Restaurant-Café de Gedinne-Centre'], ['item 4', '01.jpg', 'La terrasse', 'Avant travaux'], ['item 5', 'DSCF9387Lite.JPG', 'titre chalet vu du bas', 'titre principal ::chalet vu du bas'], ['item 6', '04_IMG_20220207_1828
56.jpg', 'Sunset', 'Entre Willerzie et Rienne'], ['item 7', '02_DSCF0225.JPG', 'Panneaux solaires', 'Les 4 premiers (total 8 en 2024)'], ['item 8', '08_IMG_20220509_193214.jpg', 'Camping', 'pas sans la RTBF'], ['item 9', 'DSCF9384Lite.JPG', '03_IMG-20220517
-WA0006.jpeg', 'La Croix-Scaille,', 'Depuis la tour du Millénaire (frontière française)'], ['item 9']]
在我的模板方面,传递简单变量和列表一切正常,但当涉及 CollectionIPTCa 变量/参数时则不行:
{% set meta1 = "une donnée info sur image" %}
{% block content %}
<div>
<img style="max-width: 40%;" src= "{{url_for('static',filename='ImagesCabWillA/07_IMG-20220325-WA0021.jpeg')}}">
Légende: {{meta1}}
</div>
<div>
<h2>CollectionIPTC</h2>
{{collectionIPTC}}
<h2>testarray</h2>
{{testarray}}
<h2>myi</h2>
{{myi}}
</div>
{% endblock content %}
谢谢,卡塞纳夫。 我将更多地使用“sys.stderr”功能。 与此同时,我又向前迈出了几步(在 PythonanyWhetre 的帮助下);希望尽快完成。 确定后,我将发布我的干净且调试过的代码。