烧瓶表单包含空值时,500内部服务器错误

时间:2016-02-16 05:50:28

标签: python flask

我已经填写了这张表格,我发布了一个名为submitRegistration的应用程序路线,代码如下......但是它返回了一些关于丢失字段的json,它投了500内部服务器错误。任何人都可以评论为什么?

@app.route('/submitRegistration',methods=['POST', 'GET'])
def submitRegistration():
    try:
    _contactName = request.form['contact_name']
    _businessName = request.form['business_name']
    _telephone = request.form['telephone']
    _email = request.form['email']
    _address = request.form['address']
    _city = request.form['city']
    _state = request.form['state']
    _zip = request.form['zip']
    _services = request.form['services']

    # validate the received values
    if _contactName and _email and _telephone and _address and _city and _state and _zip and _services:

        # All Good, let's call MySQL
        conn = mysql.connect()
        cursor = conn.cursor()
        cursor.callproc('sp_submitRegistration',(_contactName,_businessName,_telephone,_email,_address,_city,_state,_zip,_services))
        data = cursor.fetchall()

        # SMS Confirmation
        smsClient = TwilioRestClient(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
        message = smsClient.messages.create(
            body=TWILIO_REG_SMS_MESSAGE,
            to=_telephone,
            from_=TWILIO_FROM_TELEPHONE,
        )

        if len(data) is 0:
            conn.commit()
            return json.dumps({'message':'Registration submitted 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()

控制台输出

WARNING:tornado.access:404 GET /favicon.ico (2620:10d:c091:200::f:c1ec) 24.98ms
ERROR:__main__:Exception on /submitRegistration [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "app.py", line 113, in submitRegistration
    cursor.close()
UnboundLocalError: local variable 'cursor' referenced before assignment
ERROR:tornado.access:500 POST /submitRegistration (<IPV6 ADDRESS>) 17.90ms

新的应用路线......

# Functions 
@app.route('/submitRegistration',methods=['POST', 'GET'])
def submitRegistration():
    try:
    _contactName = request.form['contact_name']
    _businessName = request.form['business_name']
    _telephone = request.form['telephone']
    _email = request.form['email']
    _address = request.form['address']
    _city = request.form['city']
    _state = request.form['state']
    _zip = request.form['zip']
    _services = request.form['services']

    # validate the received values
    if _contactName and _email and _telephone and _address and _city and _state and _zip and _services != "":

            # All Good, let's call MySQL
            conn = mysql.connect()
            cursor = conn.cursor()
            cursor.callproc('sp_submitRegistration',(_contactName,_businessName,_telephone,_email,_address,_city,_state,_zip,_services)    )
            data = cursor.fetchall()

        # SMS Confirmation
        smsClient = TwilioRestClient(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
        message = smsClient.messages.create(
            body=TWILIO_REG_SMS_MESSAGE,  
            to=_telephone,
            from_=TWILIO_FROM_TELEPHONE,
            )       

        if len(data) is 0:
                conn.commit()
                return json.dumps({'message': 'Registration submitted successfully !'})
                cursor.close()
                conn.close()
        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)})    

1 个答案:

答案 0 :(得分:0)

这里回答基本上关闭if语句之外的mysql连接创建了超出范围的条件...

更正以下app.route。

# Functions 
@app.route('/submitRegistration',methods=['POST', 'GET'])
def submitRegistration():
    try:
    _contactName = request.form['contact_name']
    _businessName = request.form['business_name']
    _telephone = request.form['telephone']
    _email = request.form['email']
    _address = request.form['address']
    _city = request.form['city']
    _state = request.form['state']
    _zip = request.form['zip']
    _services = request.form['services']

    # validate the received values
    if _contactName and _email and _telephone and _address and _city and _state and _zip and _services != "":

            # All Good, let's call MySQL
            conn = mysql.connect()
            cursor = conn.cursor()
            cursor.callproc('sp_submitRegistration',(_contactName,_businessName,_telephone,_email,_address,_city,_state,_zip,_services)    )
            data = cursor.fetchall()

        # SMS Confirmation
        smsClient = TwilioRestClient(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
        message = smsClient.messages.create(
            body=TWILIO_REG_SMS_MESSAGE,  
            to=_telephone,
            from_=TWILIO_FROM_TELEPHONE,
            )       

        if len(data) is 0:
                conn.commit()
                return json.dumps({'message': 'Registration submitted successfully !'})
                cursor.close()
                conn.close()
        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)})    
相关问题