У меня проблемы с получением данных из моей базы данных для отображения внутри моей таблицы HTML с помощью Flask / Jinja2.
На этом этапе мой HTML-код выглядит следующим образом
<table class="table table-sm table-striped">
<thread class="thread-light">
<tr>
<th>Course Code:</th>
<th>Course Title:</th>
<th>Year Delivered:</th>
</tr>
</thread>
<tbody>
{% for item in data %}
<tr>
<td>{{item[0]}}</td>
<td>{{item[1]}}</td>
<td>{{item[2]}}</td>
</tr>
{% endfor %}
</tbody>
</table>
Myкод Python выглядит следующим образом
if request.method == 'POST':
course_code = request.form['coursecode']
course_title = request.form['coursetitle']
year_delivered = request.form['yeardelivered']
try:
cursor = mydb.cursor()
cursor.execute ("INSERT INTO courses (course_code, course_title, year_delivered) VALUES ('{}','{}','{}');".format(course_code, course_title, year_delivered))
mydb.commit()
cursor.close()
except mysql.connector.Error as err:
return failure('newcourse', f"Error message: {err}. Course not added")
else:
return redirect(url_for('newcourse_success'))
cursor.close()
elif request.method == 'GET':
return render_template('newcourse.html')
cursor = mydb.cursor()
cursor.execute ("SELECT * FROM courses")
data = cursor.fetchall()
cursor.close()
render_template('newcourse_success', data=data)
Я на 99% уверен, что все это связано с тем, где у меня есть код, содержащийся в моем скрипте Python, но я не совсем уверен, где это нужнобыть помещены, чтобы заставить это отображаться должным образом в таблице HTML.
Заранее спасибо!