How to pass items to loop using Jinja2/Flask? How to pass items to loop using Jinja2/Flask? flask flask

How to pass items to loop using Jinja2/Flask?


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.

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

from flask import Flask, render_templateadictionary = {'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)


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.