Jinja Error Return About For Loop

lucg

I have been trying to read text files with Flask and Jinja but using for loops, I have been running into trouble. My code is below:

{% 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 %}

The only problem is that it is returning an error:

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'.

I found that it is this causing the problem:

fileNameOpen = open(fileNamesIterator, "r")
fileLines = fileName.readlines()

But shouldn't I be able to do other python statements after for statement but before the endfor statement?

Any ideas?

Solution: Jinja is not Python and most of the work needs to be done out of the template

Sean Vieira

Jinja is not Python (if you need to be able to write arbitrary Python in your templates you will do better with Mako). Instead, you need to do the work in Python and pass the results to Jinja:

data = []
for file_name in file_names:
    with open(file_name, 'r') as f:
        file_lines = f.readlines()
        data.append({
            "id": file_name[:len(file_name) - 4],
            "title": file_lines[2],
            "author": file_lines[1],
            "date": file_lines[0],
            "content": file_lines[3:5]
        })

return render_template("template.html", articles=data)

Then your template can just be:

{% for article in articles %}
<div id="{{ article.id }}" class="title">Title: {{ article.title }}</div>
<div class="author">Author: {{ article.author }}</div>
<div class="date">Date Posted: {{ article.date }}</div>
<br><br>
<div class="mainText"> {{ article.content[0] }} <br> {{ article.content[1] }}
</div>
<hr>
{% endfor %}

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Missing return statement with for loop

분류에서Dev

The issue about the exception which occurs with return statement

분류에서Dev

Need info about return -<options> in TCL

분류에서Dev

While loop and function return with PDO

분류에서Dev

Should I be worried about this error?

분류에서Dev

PayPal: error - return to merchant

분류에서Dev

return Json syntax error

분류에서Dev

Syntax error on return

분류에서Dev

Flask and Jinja template throwing error: object of type 'Cursor' has no len()

분류에서Dev

While/for loop error

분류에서Dev

Error in while loop?

분류에서Dev

Mips code : loop error

분류에서Dev

Return as String Value from For Loop Java

분류에서Dev

php return json with angularjs error

분류에서Dev

"Missing return statement" error in Java

분류에서Dev

Bus Error on void function return

분류에서Dev

A strange error occur about allocate array and rand()

분류에서Dev

Xen error: "xl list" complains about libxl

분류에서Dev

error about assign List.element

분류에서Dev

About a type specifier in NuSMV (error: invalid subrange)

분류에서Dev

Why does the error method return an error?

분류에서Dev

Error with Python For Loop within Function

분류에서Dev

Error with Python For Loop within Function

분류에서Dev

Display error message and resume loop

분류에서Dev

weird shell script error: invalid option return: usage: return [n]

분류에서Dev

Ansible Jinja Loop를 사용할 때 마지막 문자를 다듬는 방법

분류에서Dev

ansible jinja2-다른 "for 문"에서 "loop range"출력을 사용하는 방법

분류에서Dev

jinja for loop가 생성 한 HTML 테이블의 '중첩 실수'수정

분류에서Dev

Conditional statement in React return causes syntax error