【发布时间】:2020-11-02 02:57:07
【问题描述】:
这是一个将数据插入 mysql 数据库的简单应用程序。数据已正确插入,但我没有收到我的发布请求的响应,但状态为 200.request.form 有效,但我想将 json 作为有效负载发送 这是我的以下路线,我相信当我检查密钥是否存在于我的 json 有效负载中时出现问题。我尝试使用 request.json,request.get_json,request.get_json() 但我无法摆脱这个错误虽然状态是 200 但应用程序没有返回任何消息。
@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}
这是我的以下回复:
Debugging middleware caught exception in streamed response at a point where response headers were already sent
还有这个
MySQLdb._exceptions.OperationalError: (2006, '').
【问题讨论】:
-
应用程序的其余部分是什么?当您发送请求时,网络服务器的输出是什么?
-
当我在 request.form['name'] 中执行 'name' ...... name = request.form['name] 然后它可以工作,但我想以 json 格式发送数据作为形式
标签: python python-3.x flask