2015-09-06 56 views
-2

我在使用python Flask框架運行我的應用程序時遇到了一個錯誤。在簽到形式執行validateLogin動作所註冊的app.py。我確保我的商店程序是正確的。感謝您的幫助!燒瓶得到500內部服務器錯誤

app.py

from flask import Flask, render_template, json, request 
from flask.ext.mysql import MySQL 
from werkzeug import generate_password_hash, check_password_hash 

mysql = MySQL() 
app = Flask(__name__) 

# MySQL configurations 
app.config['MYSQL_DATABASE_USER'] = 'root' 
app.config['MYSQL_DATABASE_PASSWORD'] = '12345' 
app.config['MYSQL_DATABASE_DB'] = 'BucketList' 
app.config['MYSQL_DATABASE_HOST'] = 'localhost' 
mysql.init_app(app) 


@app.route('/') 
def main(): 
    return render_template('index.html') 

@app.route('/showSignUp') 
def showSignUp(): 
    return render_template('signup.html') 


@app.route('/signUp',methods=['POST','GET']) 
def signUp(): 
    try: 
     _name = request.form['inputName'] 
     _email = request.form['inputEmail'] 
     _password = request.form['inputPassword'] 

     # validate the received values 
     if _name and _email and _password: 

      # All Good, let's call MySQL 

      conn = mysql.connect() 
      cursor = conn.cursor() 
      _hashed_password = generate_password_hash(_password) 
      cursor.callproc('sp_createUser',(_name,_email,_hashed_password)) 
      data = cursor.fetchall() 

      if len(data) is 0: 
       conn.commit() 
       return json.dumps({'message':'User created successfully !'}) 
      else: 
       return json.dumps({'error':str(data[0])}) 
     else: 
      return json.dumps({'html':'<span>Enter the required fields</span>'}) 

    except Exception as e: 
     return json.dumps({'error':str(e)}) 
    finally: 
     cursor.close() 
     conn.close() 

@app.route('/showSignIn') 
def showSignin(): 
    return render_template('signin.html') 

@app.route('/validateLogin',methods=['POST']) 
def validateLogin(): 
    try: 
     _username = request.form['inputEmail'] 
     _password = request.form['inputPassword'] 

     # connect to mysql 

     con = mysql.connect() 
     cursor = con.cursor() 
     cursor.callproc('sp_validateLogin',(_username)) 
     data = cursor.fetchall() 

     if len(data) > 0: 
      if check_password_hash(str(data[0][3]),_password): 
       session['user'] = data[0][0] 
       return redirect('/userHome') 
      else: 
       return render_template('error.html',error = 'Wrong Email address or Password.') 
     else: 
      return render_template('error.html',error = 'Wrong Email address or Password.') 


    except Exception as e: 
     return render_template('error.html',error = str(e)) 
    finally: 
     cursor.close() 
     conn.close() 

@app.route('/userHome') 
def userHome(): 
    return render_template('userHome.html') 
if __name__ == "__main__": 
    app.run(port=5000) 

signin.html

<!DOCTYPE html> 
<html lang="en"> 
    <head> 
    <title>Python Flask Bucket List App</title> 


    <link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet"> 

    <link href="http://getbootstrap.com/examples/jumbotron-narrow/jumbotron-narrow.css" rel="stylesheet"> 
    <!-- <link href="../static/css/signup.css" rel="stylesheet"> --> 
    <script src="../static/js/jquery-1.11.2.js"></script> 

    </head> 

    <body> 

    <div class="container"> 
     <div class="header"> 
     <nav> 
      <ul class="nav nav-pills pull-right"> 
      <li role="presentation" ><a href="/">Home</a></li> 
      <li role="presentation" class="active"><a href="#">Sign In</a></li> 
      <li role="presentation" ><a href="/showSignUp">Sign Up</a></li> 
      </ul> 
     </nav> 
     <h3 class="text-muted">Python Flask App</h3> 
     </div> 

     <div class="jumbotron"> 
     <h3>Bucket List Sign In</h3> 
     <form role="form" action="/validateLogin" method="post"> 
     <div class="form-group"> 
      <label for="inputEmail" class="sr-only">Email address</label> 
      <input type="email" name="inputEmail" id="inputEmail" class="form-control" placeholder="Email address" required autofocus> 
     </div> 
     <div class="form-group"> 
      <label for="inputPassword" class="sr-only">Password</label> 
      <input type="password" name="inputPassword" id="inputPassword" class="form-control" placeholder="Password" required> 
     </div> 

     <button id="btnSignIn" class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button> 
     </form> 
     </div> 



     <footer class="footer"> 
     <p>&copy; Company 2015</p> 
     </footer> 

    </div> 
    </body> 
</html> 

而且在userHome.html

<!DOCTYPE html> 
<html lang="en"> 

<head> 
    <title>Python Flask Bucket List App</title> 


    <link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet"> 

    <link href="http://getbootstrap.com/examples/jumbotron-narrow/jumbotron-narrow.css" rel="stylesheet"> 
    <!-- <link href="../static/css/signup.css" rel="stylesheet"> --> 


</head> 

<body> 

    <div class="container"> 
     <div class="header"> 
      <nav> 
       <ul class="nav nav-pills pull-right"> 
        <li role="presentation" class="active"><a href="/logout">Logout</a> 
        </li> 
       </ul> 
      </nav> 
      <h3 class="text-muted">Python Flask App</h3> 
     </div> 

     <div class="jumbotron"> 
      <h1>Welcome Home !!</h1> 

     </div> 


     <footer class="footer"> 
      <p>&copy; Company 2015</p> 
     </footer> 

    </div> 
</body> 

</html> 
+0

什麼錯誤?將錯誤的詳細信息添加到您的問題中。嘗試在啓動應用程序之前使用'app.debug = True'開啓調試。 – mhawke

+0

它已經運行,但爲什麼check_password_hash函數不能按預期工作。幫我! –

回答

0

事實上,您正在收到500內部服務器錯誤,這表明未處理的異常正在代碼中的某處引發。查看日誌文件並打開調試(我仍然不確定是否已經完成了這項工作!)。啓用調試後,異常回溯將被很好地格式化並顯示在網頁中。

爲什麼不是check_password_hash()按照您的期望工作?你期望它做什麼?我想你的意思是它沒有正確地檢查密碼?嘗試打印出從數據庫返回的值,即值爲data,特別是data[0][3]

validateLogin()函數捕獲所有異常,並假定在error.html模板中顯示異常。該頁面正在呈現?