2016-09-30 9 views
-1

私はapp.pyをMySQLデータベースにリンクさせようとしていますが、以下のエラーが表示され続けます。私のリポジトリへのリンクはこちらです:https://github.com/trhubwork/python-mysql-proj.gitなぜ私のpythonアプリケーションは500の内部サーバーエラーを取得し続けていますか?

私が実行しているのpython 3.5.1

コンソールのエラー:

jquery-1.11.2.js:9659 POST [runningonlocalhost]/signUp 500 (INTERNAL SERVER ERROR)send @ jquery-1.11.2.js:9659ajax @ jquery-1.11.2.js:9210(anonymous function) @ signUp.js:4dispatch @ jquery-1.11.2.js:4665elemData.handle @ jquery-1.11.2.js:4333 signUp.js:12 Object {readyState: 4, responseText: "↵", status: 500, statusText: "INTERNAL SERVER ERROR"}

Gitbash端子エラー:これは私のアプリは

  • Running on [localhost]:5002/ (Press CTRL+C to quit) 127.0.0.1 - - [29/Sep/2016 22:06:17] "GET/HTTP/1.1" 200 - 127.0.0.1 - - [29/Sep/2016 22:06:20] "GET /showSignUp HTTP/1.1" 200 - [2016-09-29 22:06:22,142] ERROR in app: Exception on /signUp [POST] Traceback (most recent call last): File "C:\Users\TR\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1988, in wsgi_app response = self.full_dispatch_request() File "C:\Users\TR\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1641, in full_dispatch_request rv = self.handle_user_exception(e) File "C:\Users\TR\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1544, in handle_user_exception reraise(exc_type, exc_value, tb) File "C:\Users\TR\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask_compat.py", line 33, in reraise raise value File "C:\Users\TR\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1639, in full_dispatch_request rv = self.dispatch_request() File "C:\Users\TR\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1625, in dispatch_request return self.view_functionsrule.endpoint File "app.py", line 54, in signUp cursor.close() UnboundLocalError: local variable 'cursor' referenced before assignment 127.0.0.1 - - [29/Sep/2016 22:06:22] "POST /signUp HTTP/1.1" 500 - [2016-09-29 22:09:03,808] ERROR in app: Exception on /signUp [POST] Traceback (most recent call last): File "C:\Users\TR\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1988, in wsgi_app response = self.full_dispatch_request() File "C:\Users\TR\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1641, in full_dispatch_request rv = self.handle_user_exception(e) File "C:\Users\TR\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1544, in handle_user_exception reraise(exc_type, exc_value, tb) File "C:\Users\TR\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask_compat.py", line 33, in reraise raise value File "C:\Users\TR\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1639, in full_dispatch_request rv = self.dispatch_request() File "C:\Users\TR\AppData\Local\Programs\Python\Python35-32\lib\site-packages\flask\app.py", line 1625, in dispatch_request return self.view_functionsrule.endpoint File "app.py", line 54, in signUp cursor.close() UnboundLocalError: local variable 'cursor' referenced before assignment 127.0.0.1 - - [29/Sep/2016 22:09:03] "POST /signUp HTTP/1.1" 500 -

です.py

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

mysql = MySQL() 
app = Flask(__name__) 

# MySQL configurations 
app.config['MYSQL_DATABASE_USER'] = 'jay' 
app.config['MYSQL_DATABASE_PASSWORD'] = 'jay' 
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() 

if __name__ == "__main__": 
    app.run(port=5002) 

    enter code here 
あなたのコードは、その ifステートメントを入力しない場合は

Signup.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> 
    <script src="../static/js/signUp.js"></script> 

    </head> 

    <body> 

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

     <div class="jumbotron"> 
     <h1>Bucket List App</h1> 
     <form class="form-signin"> 
     <label for="inputName" class="sr-only">Name</label> 
     <input type="name" name="inputName" id="inputName" class="form-control" placeholder="Name" required autofocus> 
     <label for="inputEmail" class="sr-only">Email address</label> 
     <input type="email" name="inputEmail" id="inputEmail" class="form-control" placeholder="Email address" required autofocus> 
     <label for="inputPassword" class="sr-only">Password</label> 
     <input type="password" name="inputPassword" id="inputPassword" class="form-control" placeholder="Password" required> 

     <button id="btnSignUp" class="btn btn-lg btn-primary btn-block" type="button">Sign up</button> 
     </form> 
     </div> 



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

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

signUp.js

$(function(){ 
    $('#btnSignUp').click(function(){ 

     $.ajax({ 
      url: '/signUp', 
      data: $('form').serialize(), 
      type: 'POST', 
      success: function(response){ 
       console.log(response); 
      }, 
      error: function(error){ 
       console.log(error); 
      } 
     }); 
    }); 
}); 
+1

ifでカーソルを定義します。コードがifに入ることがないなら、どうすればそれを閉じることができますか? – epascarello

答えて

0

@epascarelloが正しいとは、そしてあなたも、カーソルを作成することは決してないだろう。私はtryfinallyのブロックが良い考えだと思います。

try: 
    conn = mysql.connect() 
    cursor = conn.cursor() 
    _name = request.form['inputName'] 
    _email = request.form['inputEmail'] 
    _password = request.form['inputPassword'] 

    # ... 
    # contents of the if and else statements here 
    # ... 

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

@ caralvとepascarelloのコメントありがとうございます。私はあなたの提案を試みたが、エラーは起こり続ける。 – ClydeCup

関連する問題