我正在嘗試在郵遞員中測驗燒瓶 API 的端點,但我在下面遇到此錯誤
型別錯誤:視圖函式未回傳有效的回應元組。元組必須具有 (body, status, headers)、(body, status) 或 (body, headers) 的形式。
下面給出函式
auth = Blueprint("auth", __name__, url_prefix="/api/v1/auth")
@auth.post('/register')
def register():
username = request.json['username']
password = request.json['password']
email = request.json['email']
if len(password) < 6:
return jsonify({'error': "Password is too short"}),
HTTP_400_BAD_REQUEST
if len(username) < 3:
return jsonify({'error': "User is too short"}),
HTTP_400_BAD_REQUEST
if username.isalnum() or " " in username:
return jsonify({'error': "User is too short"}),
HTTP_400_BAD_REQUEST
if not username.isalnum() or " " in username:
return jsonify({'error': "Username should be alphanumeric, also no spaces"}),
HTTP_400_BAD_REQUEST
if not validators.email(email):
return jsonify({'error': "Email is not valid"})
HTTP_400_BAD_REQUEST
if User.query.filter_by(email=email).first() is not None:
return jsonify({'error': "Email is taken"}), HTTP_409_CONFLICT
if User.query.filter_by(username=username).first() is not None:
return jsonify({'error': "username is taken"}), HTTP_409_CONFLICT
pwd_hash=generate_password_hash(password)
user = User(username=username, password=pwd_hash, email=email)
db.session.add(user)
db.session.commit()
return jsonify({
'message': "User created",
'user': {
'username': username, 'email': email
}
}), HTTP_201_CREATED
郵遞員對路由http://127.0.0.1:5000/api/v1/auth/test的測驗輸入是:
{
"username": "username",
"password": "password",
"email": "[email protected]"
}
uj5u.com熱心網友回復:
好吧你忘了,在下面的代碼中添加一個
if not validators.email(email):
return jsonify({'error': "Email is not valid"})
HTTP_400_BAD_REQUEST
這導致了這個型別錯誤
只需將其更改為
return jsonify({'error': "Email is not valid"}),
HTTP_400_BAD_REQUEST
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/537093.html
標籤:Python烧瓶
