Reputation: 820
I'm relatively new to Python/flask and I'm having trouble with some database stuff.
I was able to connect to a MySQL db in a different function and get form inputs from a user inserted into the db using wtforms. Now I want to display the data stored in the db on the 'dashboard' page in table format using jinja templating. Every time someone submits a new form, I want the table to expand to include that data.
However, nothing seems to be showing up at all, even the existing data which leads me to believe I'm doing something wrong in the second code snippet below. I'm not getting any errors when I run the python file so perhaps I'm doing the HTML templating wrong?
Database connection (dbconnect.py):
import MySQLdb
def connection():
# Edited out actual values
conn = MySQLdb.connect(host="",
user="",
passwd="",
db = "")
c = conn.cursor()
return c, conn
Main python code (don't know if try/except is necessary):
from dbconnect import connection
from flask import Flask, render_template
@app.route('/dashboard/')
def display_deals():
try:
c, conn = connection()
query = "SELECT * from submissions"
c.execute(query)
data = c.fetchall()
conn.close()
return data
return render_template("dashboard.html", data=data)
except Exception as e:
return (str(e))
HTML:
<table border="1" cellpadding="5" cellspacing="5">
{% for row in data %}
<tr>
{% for d in row %}
<td>{{ d }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
Upvotes: 1
Views: 13422
Reputation: 6495
You have a return
statement before return the rendered template file.
Upvotes: 1