NameError: global name 'hashed_pwd' is not defined

930 views Asked by At

i am new with python and Flask .I make a login and signup page and everything is working fine.Now, i use werkzeug for password security at signup page password is generated in encrypted form and stored in database but when i tried to login then there i got an error "NameError: global name 'hashed_pwd' is not defined" please provide me solution.

@app.route('/signUp', methods=['POST','GET'])
def signUp():   
        _name = request.form['user_name']        #database connectivity
        _password = request.form['user_password']
        _pname = request.form['patient_name']
        _email = request.form['user_email']
        _cont = request.form['user_contact']
        _add = request.form['user_address']

        if _name and _password and _pname and _email and _cont and _add:
            conn = mysql.connect()
            cursor = conn.cursor()
            hashed_pwd=generate_password_hash(_password)    #password generated
            query_string = """INSERT INTO tbl_puser (user_name,user_password,patient_name,user_email,user_contact,user_address) VALUES('%s','%s','%s','%s','%s','%s');"""%(_name,hashed_pwd,_pname,_email,_cont,_add)
            print query_string
            cursor.execute(query_string)
            conn.commit()
            cursor.close()
            conn.close()
            flash('You were successfully registered')
            return render_template('select.html')


@app.route('/Login',methods=['POST','GET'])
def login():

    user_name=request.form['uname']
    user_password=request.form['psw']
    # NameError: global name 'hashed_pwd' is not defined error 
    check_password_hash(hashed_pwd,user_password) 

        if user_name and user_password:
            conn = mysql.connect()
            cursor = conn.cursor()
            cursor.execute("SELECT * from tbl_puser where user_name='" + user_name + "' and user_password='"+ user_password +"'")

            print "SELECT * from tbl_puser where user_name='" + user_name + "' and user_password='"+ user_password +"';"

            data = cursor.fetchall()
            print data
            if len(data) != 0:
                return render_template("select.html")
            else:
                return redirect(url_for('index'))

if __name__ == '__main__':
    app.run()
1

There are 1 answers

1
AudioBubble On

you simple forgot defined it

user_password = request.form.get('psw', '')
# Add next line
hashed_pwd = generate_password_hash(user_password)
check_password_hash(hashed_pwd,user_password)

and better use get method of the request.form to get the values of items.