2017-08-31 108 views
-1

我是新的python和Flask。我做了一個登錄和註冊頁面,一切工作正常。現在,我使用werkzeug在註冊頁面的密碼安全性密碼生成加密表單和存儲在數據庫中,但是當我試圖登錄然後我得到一個錯誤「名稱錯誤:全球名稱hashed_pwd'未定義」請提供我的解決方案。NameError:未定義全局名稱'hashed_pwd'

@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

你正在用'hashed_pwd'變量調用'check_password_hash'。從代碼片段,看起來像它沒有被定義在任何地方 – Vinny

回答

1

你簡單的忘記將它定義

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

request.form的更好地利用get方法來獲取項目的值。

+0

謝謝,爲解決方案,但我得到了一個新的錯誤「BuildError:無法建立端點'索引'的網址,你的意思是'index_page'? – shashank

相關問題