title: jinja2 template takes over 10 secs to render, how to optimize?
body: <p>In my flask web app, there is a route that display either all the member with their first name starting with 'A, B, C, etc..' or the Show All button that shows all the members registered .  Right now it sum up to about 750 users.  The problem is that displaying the 'show all' list takes over 10 seconds.  The request to the DB itself is fast, it's the rendering that takes all those seconds.  I was wondering if there is a way to speed this up?  I'm pretty new to python, flask and jinja2 so I don't know the optimiztion paths yet.</p>
  
  <p>Here is the views.py, when I click the 'Show all" button on the webpage, it calls the url with the letter argument set to '0':</p>
  
  <pre><code>@main.route('/all-members')
  @login_required
  @admin_required
  def all_members():
      alphabet = list(string.ascii_lowercase)
      first_letter = request.args.get('letter')
      if first_letter == '0':
          user_ = User.query.order_by(User.fullname.asc()).all()
      else:
          user_ = User.query.filter(User.username.startswith(first_letter)).order_by(User.fullname.asc()).all()
      return render_template('all_members.html', all_members = user_, alphabet = alphabet)
  </code></pre>
  
  <p>It return 750 user objects.  I then pass that result to the template:</p>
  
  <pre><code>    return render_template('all_members.html', all_members = user_, alphabet = alphabet)
  </code></pre>
  
  <p>And the template:</p>
  
  <pre><code>{% extends "base.html" %}
  
  {% block title %}Zata - members list{% endblock %}
  
  {% block page_content %}
  &lt;div class="page-header" xmlns="http://www.w3.org/1999/html"&gt;
      &lt;h1&gt;Tribe members list&lt;/h1&gt;
  &lt;p&gt;&lt;/p&gt;
  {% for letter in alphabet %}
   &lt;a class="btn btn-success" href="{{ url_for('.all_members', letter = letter) }}"&gt;{{ letter }}&lt;/a&gt;
  {% endfor %}
  &lt;a class="btn btn-info" href="{{ url_for('.all_members', letter = 0) }}"&gt;Show All&lt;/a&gt;
  &lt;/div&gt;
  {% for member in all_members %}
  
      &lt;div class="row"&gt;
  &lt;p&gt;
          &lt;div class="col-md-2"&gt;
              &lt;a href="{{ url_for('main.member',username=member.username) }}"&gt;{{ member.fullname }}&lt;/a&gt;
          &lt;/div&gt;
          &lt;div class="col-md-1"&gt;
              &lt;a class="btn btn-warning btn-xs" href="{{ url_for('.edit_profile_admin', id=member.id) }}"&gt; Edit Profile&lt;/a&gt;
          &lt;/div&gt;
          &lt;div class="col-md-3"&gt;
              &lt;a class="btn btn-info btn-xs" href="{{ url_for('.add_punchcard', id=member.id, next=request.url) }}"&gt; Manage Punchcard&lt;/a&gt;
              {% if member.punchcard_gold %}&lt;span class="btn btn-xs"  style="background-color:gold"&gt;G:{{ member.punchcard_gold }}&lt;/span&gt;{% endif %}
              {% if member.punchcard_normal %}&lt;span class="btn btn-xs"  style="background-color:#c0c0c0"&gt;R:{{ member.punchcard_normal }}&lt;/span&gt;{% endif %}
          &lt;/div&gt;
          &lt;div class="col-md-4"&gt;
              &lt;a class="btn btn-info btn-xs" href="{{ url_for('.add_seasonpass', id=member.id, next=request.url) }}"&gt; Manage Season pass&lt;/a&gt;
              {% if member.season_pass_gold %}&lt;span class="btn btn-xs"  style="background-color:gold"&gt;G:{{ member.season_pass_gold }}&lt;/span&gt;{% endif %}
              {% if member.season_pass_normal %}&lt;span class="btn btn-xs"  style="background-color:#c0c0c0"&gt;R:{{ member.season_pass_normal }}&lt;/span&gt;{% endif %}
          &lt;/div&gt;
          &lt;div class="col-md-2"&gt;
               &lt;span class="btn btn-xs"  style="background-color:yellowgreen"&gt;Class attended: {{ member.class_attended|length }}
          &lt;/div&gt;
      &lt;/p&gt;
      &lt;/div&gt;
  
  
  
  {% endfor %}
  
  {% endblock %}
  </code></pre>
  
  <p>The slow part is obviously the {% for member in all_members %} but is there another way to go through the list of user and build the page?</p>