title: Why is my python app continues to get a 500 Internal Server Error?
body: <p>I'm trying to get my app.py to link with MySQL database but I keep getting the following errors shown below.  The link to my repository is here: <a href="https://github.com/trhubwork/python-mysql-proj.git" rel="nofollow">https://github.com/trhubwork/python-mysql-proj.git</a></p>
  
  <p>I'm running python 3.5.1</p>
  
  <p>Console Errors:</p>
  
  <blockquote>
    <p>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"}</p>
  </blockquote>
  
  <p>Gitbash terminal Errors:</p>
  
  <blockquote>
    <ul>
    <li>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 -</li>
    </ul>
  </blockquote>
  
  <p>This is my app.py</p>
  
  <pre><code>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':'&lt;span&gt;Enter the required fields&lt;/span&gt;'})
  
      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
  </code></pre>
  
  <p>Signup.html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
    &lt;head&gt;
      &lt;title&gt;Python Flask Bucket List App&lt;/title&gt;
  
  
      &lt;link href="http://getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet"&gt;
  
      &lt;link href="http://getbootstrap.com/examples/jumbotron-narrow/jumbotron-narrow.css" rel="stylesheet"&gt;
      &lt;link href="../static/css/signup.css" rel="stylesheet"&gt;
      &lt;script src="../static/js/jquery-1.11.2.js"&gt;&lt;/script&gt;
      &lt;script src="../static/js/signUp.js"&gt;&lt;/script&gt;
  
    &lt;/head&gt;
  
    &lt;body&gt;
  
      &lt;div class="container"&gt;
        &lt;div class="header"&gt;
          &lt;nav&gt;
            &lt;ul class="nav nav-pills pull-right"&gt;
              &lt;li role="presentation" &gt;&lt;a href="showHome"&gt;Home&lt;/a&gt;&lt;/li&gt;
              &lt;li role="presentation"&gt;&lt;a href="#"&gt;Sign In&lt;/a&gt;&lt;/li&gt;
              &lt;li role="presentation" class="active"&gt;&lt;a href="#"&gt;Sign Up&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/nav&gt;
          &lt;h3 class="text-muted"&gt;Python Flask App&lt;/h3&gt;
        &lt;/div&gt;
  
        &lt;div class="jumbotron"&gt;
          &lt;h1&gt;Bucket List App&lt;/h1&gt;
          &lt;form class="form-signin"&gt;
          &lt;label for="inputName" class="sr-only"&gt;Name&lt;/label&gt;
          &lt;input type="name" name="inputName" id="inputName" class="form-control" placeholder="Name" required autofocus&gt;
          &lt;label for="inputEmail" class="sr-only"&gt;Email address&lt;/label&gt;
          &lt;input type="email" name="inputEmail" id="inputEmail" class="form-control" placeholder="Email address" required autofocus&gt;
          &lt;label for="inputPassword" class="sr-only"&gt;Password&lt;/label&gt;
          &lt;input type="password" name="inputPassword" id="inputPassword" class="form-control" placeholder="Password" required&gt;
  
          &lt;button id="btnSignUp" class="btn btn-lg btn-primary btn-block" type="button"&gt;Sign up&lt;/button&gt;
        &lt;/form&gt;
        &lt;/div&gt;
  
  
  
        &lt;footer class="footer"&gt;
          &lt;p&gt;&amp;copy; Company 2015&lt;/p&gt;
        &lt;/footer&gt;
  
      &lt;/div&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>signUp.js</p>
  
  <pre><code>$(function(){
      $('#btnSignUp').click(function(){
  
          $.ajax({
              url: '/signUp',
              data: $('form').serialize(),
              type: 'POST',
              success: function(response){
                  console.log(response);
              },
              error: function(error){
                  console.log(error);
              }
          });
      });
  });
  </code></pre>