0

I need to greet user on the page. F.ex: Hello {{ name }}, but I get the error UnboundLocalError: local variable 'account' referenced before assignment. What is the problem in the code below?

python:

app = Flask(__name__)

mysql = MySQL(app)


@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        name = request.form['name']
        email = request.form['email']
        cur = mysql.connection.cursor()
        cur.execute('INSERT INTO users(name, email) VALUES(%s, %s)', (name, email))
        mysql.connection.commit()
        cur.close()
        return redirect('profile')

    return render_template('index.html')


@app.route('/profile', methods=['GET'])
def profile():
    if request.method == 'POST':
        name = request.form['name']
        email = request.form['email']
        cur = mysql.connection.cursor()
        cur.execute('SELECT * FROM users')

        account = cur.fetchone()

    return render_template('profile.html', account=account)

index.html:

<form action="" method="POST">
    <span style="color: #fff;">Firstname:</span><input type="text" name="name" placeholder="Type your firstname"><br><br>
    <input type="submit" name="submit" value="submit">
</form>

profile.html

<h4 style="color: #fff;">Your firstname: is {{ account['name'] }}</h4>
<h4 style="color: #fff;">Your email: is {{ account['email'] }}</h4>

I can connect to database and fetch users data, but on the profile.html page I get the error How to solve it? Please help.

2
  • Have you tried the search engine? It's really good. stackoverflow.com/questions/9845102/using-mysql-in-flask Commented Jun 5, 2020 at 16:45
  • @Hoppo, could you please check again the question? Commented Jun 5, 2020 at 17:38

1 Answer 1

0

You haven't passed the account to the template.

Instead of ,

return render_template('profile.html')

you need to write as,

return render_template('profile.html', account=account)

EDIT:

@app.route('/profile', methods=['GET','POST'])
def profile():
    if request.method == 'POST':
        name = request.form['name']
        email = request.form['email']
        cur = mysql.connection.cursor()
        cur.execute('SELECT * FROM users')

        account = cur.fetchone()

        return render_template('profile.html', account=account)

Or if you wanted the profile to be a get request you can do this

@app.route('/profile', methods=['GET','POST'])
def profile():
    if request.method == 'POST':
        name = request.form['name']
        email = request.form['email']
    cur = mysql.connection.cursor()
    cur.execute('SELECT * FROM users')

    account = cur.fetchone()

    return render_template('profile.html', account=account)
Sign up to request clarification or add additional context in comments.

9 Comments

and now I get this error UnboundLocalError: local variable 'account' referenced before assignment
can you correct the indentation as i have edited the answer :)
and make sure to enable the POST method in the route too.
and now I get this errorThe view function did not return a valid response. The function either returned None or ended without a return statement. It doesnt see profile.html.
check the latest edits :) I didn't know you were checking in GET requests. Try it now.
|