这里头痛。我有一个想要测试的简单API,但到目前为止还不能。它部分起作用。如果用户被阻止或删除,则会发生错误,提示:
pre_delete
但是我认为我确实在所有情况下都使用JsonResponse进行响应。更糟糕的是,它对于正确的用户以及不存在的电子邮件均正确运行。从邮递员处检查时,它会以状态400和正确的消息正确响应。我的考试中缺少什么吗?
'str' object has no attribute status_code.
我的测试:
class ForgotPwd(APIView):
permission_classes = []
def post(self,request, format = None):
email = request.data.get('email' , '').strip()
reply = {}
status = 400
filters = {}
if email:
filters['email'] = email
try:
user_info = User.objects.values('id', 'status', 'email').get(**filters)
if user_info['status'] == 0:
reply['detail'] = _('userBlocked')
elif user_info['status'] == 2:
reply['detail'] = _('userMissing')
else:
# send email
reply['detail'] = _('pwdResetLinkSent')
status = 200
except BaseException as e:
reply['detail'] = _('errorNoAccount')
else:
reply['detail'] = _('errorNoAccount')
return JsonResponse(reply,status=status)