Created
September 4, 2022 11:38
-
-
Save rcastellotti/83563d053367abb6c18b4dd37e80b87f to your computer and use it in GitHub Desktop.
flask jinja+sqlalchemy iterate through entire table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.route("/orders", methods=["GET", "POST"]) | |
def orders(): | |
orders = Order.query.all() | |
return render_template("orders.html", orders=orders) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{%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