I'm retrieving data from a database and I want to put it into a table. I'm using SQLAlchemy and an SQLite database.
Here is the code I have in my routes.py:
headings = ('Name', 'Code')
classes = tuple(list(ClassName.query.filter_by(id=current_user.id).all()))
print(classes)
return render_template("view.html", headings=headings, classes=classes)
Here is the code in view.html:
<div class="view-table">
<table>
<tr>
{% for header in headings %}
<th>{{ header }}</th>
{% endfor %}
</tr>
{% for row in classes %}
<tr>
{% for cell in row %}
<td>{{ cell }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</div>
Whenever I run it, I get this error:
TypeError: 'Class' object is not iterable
I was following this tutorial which is why I changed the list into a tuple: https://youtu.be/mCy52I4exTU
CodePudding user response:
I found the answer to this.
In my routes.py
, I did this:
classes = Classes.query.filter_by(classAdminID=current_user.id).all()
In my view.html
, I did this:
<div class="view-table">
<table>
<tr>
{% for header in headings %}
<th>{{ header }}</th>
{% endfor %}
</tr>
{% for row in classes %}
<tr>
<td>{{ row.Name }}</td>
<td>{{ row.Code }}</td>
</tr>
{% endfor %}
</table>
</div>
I found the solution in this video: https://youtu.be/hbDRTZarMUw