title: How to pass items to loop using Jinja2/Flask?
body: <p>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 <a href="http://pycourse.com/posts/2013/Jul/16/Templating%20and%20Jinja2/" rel="noreferrer">this tutorial</a>. However when I try to pass the list and try to loop over it, it does not appear in the rendered html.</p>
  
  <p><em>What am I doing wrong? How to properly pass the list and loop over it using <code>base.html</code> as a template?</em></p>
  
  <p>Here is my code with a dummy page list hardcoded:</p>
  
  <p><code>app.py</code>:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>And <code>base.html</code>, located in <code>/templates/</code>:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Test&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;h1&gt;All the nice pages&lt;/h1&gt;
  {% for page in pages %}
  &lt;p&gt;{{ page }}&lt;/p&gt;
  {% endfor %}
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>When I run the app and browse to <code>http://127.0.0.1:8000/</code>, this is what I get:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Test&lt;/title&gt;
  &lt;/head&gt;
  
  &lt;h1&gt;All the nice pages&lt;/h1&gt;
  &lt;body&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>