我一直在尝试阅读与Flask和Jinja的文本文件,但使用for循环,我一直在遇到麻烦。我的代码如下:Jinja错误返回关于For循环
{% for fileNamesIterator in fileNames
fileNameOpen = open(fileNamesIterator, "r")
fileLines = fileName.readlines() %}
<div id="{{ fileNamesIterator[:len(fileNamesIterator)-4] }}" class="title">Title: {{ fileLines[2] }}</div>
<div class="author">Author: {{ fileLines[1] }}</div>
<div class="date">Date Posted: {{ fileLines[0] }}</div>
<br><br>
<div class="mainText"> {{ fileLines[3] }} <br> {{ fileLines[4] }}
</div>
<hr>
{% fileLines.close()
endfor %}
唯一的问题是,它返回一个错误:
TemplateSyntaxError: Encountered unknown tag 'fileLines'. Jinja was looking for the following tags: 'endfor' or 'else'. The innermost block that needs to be closed is 'for'.
我发现,正是这个问题引起:
fileNameOpen = open(fileNamesIterator, "r")
fileLines = fileName.readlines()
但不该” t我能够在for
声明之后但在endfor
声明之前做其他Python语句吗?
任何想法?
解决方案:神社不的Python和大部分的工作需要做出来的模板