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.
What am I doing wrong? How to properly pass the list and loop over it using base.html as a template?
Here is my code with a dummy page list hardcoded:
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/:
<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:
<html>
<head>
<title>Test</title>
</head>
<h1>All the nice pages</h1>
<body>
</body>
</html>
app.pyafter starting the application? You'll need to reload it. I'd suggest addingdebug=Trueto your call toapp.run().h1appears outside thebodytag.