1

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>

1 Answer 1

1

You have a return statement before return the rendered template file.

Sign up to request clarification or add additional context in comments.

1 Comment

The return statement was in fact causing me issues. Also turns out the bigger problem was that the function wasn't executing at all because I had another function preceding it that was using '@app.route('/dashboard/')' which I now assume isn't allowed. The solution was to combine the two functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.