I am trying to send the results of an SQLite query to a Jinja2 template through Flask. To do this I execute .fetchall() on my query, then I pass the result of that to render_template() as an argument. On the html template, I iterate through each row that was passed in order to show the 'title' attribute of each row. The expected behavior is that the final html will be populated with the data passed through render_template(). I'm pretty sure I've done this exactly as it is shown on the tutorial project in the official Flask docs.
This is the code that is fetching the rows from the database.
@bp.route('/', methods=('GET',))
def archive():
"""Returns a page with all post titles, each one is a link to it's respective blog page"""
db = get_db()
rows = db.execute('SELECT id, title, Timestamp FROM blog ORDER BY Timestamp DESC').fetchall()
return render_template('archive.html', rows=rows)
And here is the Jinja2 template I am trying to populate
<!--Template for archive page; displays a link to every post on the blog-->
{% extends 'blog.html' %}
{% block header %}
<h1>All Posts<h1>
{% endblock %}
{% block body %}
{% for row in rows %}
{% if row['title'] %}
<h2>{{ row['title'] }}</h2>
{% endif %}
{% endfor %}
{% endblock %}
Edit: Here is my get_db() method
def get_db():
if 'db' not in g:
g.db = sqlite3.connect(
current_app.config['DATABASE'],
detect_types=sqlite3.PARSE_DECLTYPES
)
g.db.row_factory = sqlite3.Row
return g.db