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.

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

1 Answers1

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)
  • and now I get this error `UnboundLocalError: local variable 'account' referenced before assignment` –  Jun 05 '20 at 17:55
  • can you correct the indentation as i have edited the answer :) – Mahesh C. Regmi Jun 05 '20 at 17:57
  • and make sure to enable the POST method in the route too. – Mahesh C. Regmi Jun 05 '20 at 17:58
  • and now I get this error`The view function did not return a valid response. The function either returned None or ended without a return statement.` It doesnt see profile.html. –  Jun 05 '20 at 17:59
  • check the latest edits :) I didn't know you were checking in GET requests. Try it now. – Mahesh C. Regmi Jun 05 '20 at 18:02
  • in the last version profile.html opens, but there is no any text. –  Jun 05 '20 at 18:04
  • make sure entries exist in the users table of the database :) and they have name and email fields. – Mahesh C. Regmi Jun 05 '20 at 18:05
  • there is no problem with db. I have checked it, I can `add` data there, but can `get` only first row. How to get new row for every new user? –  Jun 05 '20 at 18:22
  • do you have any primary key ( integer ) which get incremented when new user is added ? if your field is named as id, you can do like this. change id as you wish. change the `cur = mysql.connection.cursor()` into `cur.execute('SELECT * FROM users ORDER BY id DESC')` – Mahesh C. Regmi Jun 05 '20 at 18:26