Issue
This is an simple application that inserts data into a mysql db. The data gets inserted correctly but i am not getting the reponse of my post request but the status is 200.request.form works but i want to send json as payload This is my below route and i believe something is wrong when i am checking if the keys exist in my json payload. i tried using request.json,request.get_json,request.get_json() but i am unable to get rid of this error although the status is 200 but application is not returning any message.
@app.route('/pythonlogin/register', methods=['GET', 'POST'])
def register():
# Output message if something goes wrong..
msg = ''
# current_time = time.strftime('%Y-%m-%d %H:%M:%S')
# Check if "username", "password" and "email" POST requests exist (user submitted form)
if request.method == 'POST' and request.get_json().get('name') != None and request.get_json().get('password') != None and request.get_json().get('email') != None and request.get_json().get('size')!= None :
data = request.get_json().get
# Create variables for easy access
name = data('name')
password = data('password')
email = data('email')
size = data('size')
# Check if account exists using MySQL
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM user WHERE email = %s', (email,))
account = cursor.fetchone()
print(account)
# If account exists show error and validation checks
if account:
msg = 'Account already exists!'
elif not re.match(r'[^@]+@[^@]+\.[^@]+', email):
msg = 'Invalid email address!'
elif not re.match(r'[A-Za-z0-9]+', name):
msg = 'Username must contain only characters and numbers!'
elif not re.match(r'[0-9]',size):
msg = 'Team size accepts only numbers'
elif not name or not password or not email or not size:
msg = 'Please fill out the form!'
else:
print('inside else')
role = 1
# Account doesnt exists and the form data is valid, now insert new account into accounts table
cursor.execute('INSERT INTO company VALUES (NULL, %s,NULL, %s, %s)', (name, size,current_time))
#cursor.execute('INSERT INTO user VALUES (NULL, %s, %s, NULL,NULL, NULL,NULL, NULL,NULL, NULL)', (email, password,))
mysql.connection.commit()
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute('SELECT * FROM company WHERE name = %s ' ,(name,))
results = cursor.fetchone()
comp_id = results['company_id']
cursor.execute('INSERT INTO user VALUES (NULL, NULL, %s,%s, %s,NULL, NULL,NULL, %s,%s)', (email, password,role,current_time,comp_id,))
mysql.connection.commit()
msg = 'You have successfully registered!'
else :
# Form is empty... (no POST data)
msg = 'Please fill out the form!'
return {"status":"1","message":msg}
this is my below response:
Debugging middleware caught exception in streamed response at a point where response headers were already sent
and this
MySQLdb._exceptions.OperationalError: (2006, '').
Solution
Eventually i declared this twice
mysql = MySQL(app)
Answered By - DataDoctor
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.