2017-08-31 37 views
-1

私はPythonとFlaskで新しいです。ログインとサインアップページを作成して、すべてうまくいきました。今、サインアップページのパスワードセキュリティにwerkzeugを使用します。フォームに格納され、データベースに格納されていますが、私がログインしようとすると、 "NameError:グローバル名 '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

あなたは 'check_password_hash'を' hashed_pwd'変数で呼んでいます。コードスニペットからはどこにも定義されていないようですが、 – 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'のURLをビルドできませんでした。代わりに 'index_page'を意味しましたか? – shashank

関連する問題