如何使用Jinja2 / Flask将项目传递给循环?

时间:2020-12-11 20:50:26

I want to pass a list of pages and loop it in Jinja2 to show all pages of my website. I use Flask to construct and run the app. I followed the official flask documentation, together with this tutorial. However when I try to pass the list and try to loop over it, it does not appear in the rendered html.

我想传递一个页面列表并在Jinja2中循环它以显示我网站的所有页面。我使用Flask构建并运行应用程序。我跟着官方的烧瓶文档,以及本教程。但是,当我尝试传递列表并尝试循环它时,它不会出现在渲染的html中。

What am I doing wrong? How to properly pass the list and loop over it using base.html as a template?

我究竟做错了什么?如何使用base.html作为模板正确传递列表并循环遍历它?

Here is my code with a dummy page list hardcoded:

这是我的代码,其中包含一个硬编码的虚拟页面列表:

app.py:

app.py:

from flask import Flask, render_template

app = Flask(__name__)


@app.route('/')
def index():
    page_list = ['Eins', 'Zwei']
    return render_template('base.html', pages=page_list)


if __name__ == "__main__":
    app.run(port=8000)

And base.html, located in /templates/:

和base.html,位于/ templates /:

<html>
<head>
<title>Test</title>
</head>

<body>
<h1>All the nice pages</h1>
{% for page in pages %}
<p>{{ page }}</p>
{% endfor %}
</body>
</html>

When I run the app and browse to http://127.0.0.1:8000/, this is what I get:

当我运行应用程序并浏览到http://127.0.0.1:8000/时,这就是我得到的:

<html>
<head>
<title>Test</title>
</head>

<h1>All the nice pages</h1>
<body>

</body>
</html>

2 个解决方案

#1


2  

This code is totally valid. The important thing is to restart the server if you do changes to lists or dictionaries.

此代码完全有效。重要的是如果对列表或词典进行更改,请重新启动服务器。

Apart from that, in Flask you can pass any type that is built into Python, be it a list, a dictionary or a tuple.

除此之外,在Flask中你可以传递任何内置于Python中的类型,无论是列表,字典还是元组。

Here are short example for each of the types that pass more or less the same content:

以下是传递或多或少相同内容的每种类型的简短示例:

from flask import Flask, render_template

adictionary = {'spam': 1, 'eggs': 2}
alist = ['Eins', 'Zwei', 'Drei']
atuple = ('spam', 'eggs')

app = Flask(__name__)


@app.route('/')
def index():
    return render_template('base.html', pages=alist)


@app.route('/tuple/')
def tuple():
    return render_template('base.html', pages=atuple)


@app.route('/dict/')
def adict():
    return render_template('base.html', pages=adictionary)

if __name__ == "__main__":
    app.run(port=8000)

#2


0  

I was having this same issue. I use Sublime Text 3 and realized that I didn't automatically convert tabs to spaces. Once I made that change in the user settings, and reran the script, it output the list correctly.

我遇到了同样的问题。我使用Sublime Text 3并意识到我没有自动将标签转换为空格。一旦我在用户设置中进行了更改,并重新编写脚本,它就会正确输出列表。

#1


2  

This code is totally valid. The important thing is to restart the server if you do changes to lists or dictionaries.

此代码完全有效。重要的是如果对列表或词典进行更改,请重新启动服务器。

Apart from that, in Flask you can pass any type that is built into Python, be it a list, a dictionary or a tuple.

除此之外,在Flask中你可以传递任何内置于Python中的类型,无论是列表,字典还是元组。

Here are short example for each of the types that pass more or less the same content:

以下是传递或多或少相同内容的每种类型的简短示例:

from flask import Flask, render_template

adictionary = {'spam': 1, 'eggs': 2}
alist = ['Eins', 'Zwei', 'Drei']
atuple = ('spam', 'eggs')

app = Flask(__name__)


@app.route('/')
def index():
    return render_template('base.html', pages=alist)


@app.route('/tuple/')
def tuple():
    return render_template('base.html', pages=atuple)


@app.route('/dict/')
def adict():
    return render_template('base.html', pages=adictionary)

if __name__ == "__main__":
    app.run(port=8000)

#2


0  

I was having this same issue. I use Sublime Text 3 and realized that I didn't automatically convert tabs to spaces. Once I made that change in the user settings, and reran the script, it output the list correctly.

我遇到了同样的问题。我使用Sublime Text 3并意识到我没有自动将标签转换为空格。一旦我在用户设置中进行了更改,并重新编写脚本,它就会正确输出列表。