Skip to content

Instantly share code, notes, and snippets.

@rcastellotti
Created September 4, 2022 11:38
Show Gist options
  • Save rcastellotti/83563d053367abb6c18b4dd37e80b87f to your computer and use it in GitHub Desktop.
Save rcastellotti/83563d053367abb6c18b4dd37e80b87f to your computer and use it in GitHub Desktop.
flask jinja+sqlalchemy iterate through entire table
@app.route("/orders", methods=["GET", "POST"])
def orders():
orders = Order.query.all()
return render_template("orders.html", orders=orders)
{%extends "base.html"%} {%block content %}
<div class="border overflow-x-auto relative shadow-md rounded">
<table class="w-full text-xs lg:text-sm text-left text-gray-800 table-auto">
<thead class="border-b text-gray-700 uppercase bg-gray-50">
<tr>
{% for col in orders[0].__table__.columns%}
<th scope="col" class="px-2 py-1">{{col}}</th>
{%endfor%}
</tr>
</thead>
<tbody>
{% for order in orders %}
<tr class="even:bg-gray-100 bg-white border-b hover:bg-gray-50">
{% for k,v in order.as_dict().items()%}
<td scope="row" class="px-2">{{v}}</td>
{%endfor%}
</tr>
{%endfor%}
</tbody>
</table>
</div>
{%endblock%}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment