我正在尝试为此自定义电子邮件例外:
https://github.com/anx-ckreuzberger/django-rest-passwordreset
我尝试使用redirect()
和requests.post()
:
response = redirect(django_rest_passwordreset.urls)
response = requests.post(django_rest_passwordreset.urls (try convert to url), data=request.data) #it's stupid...
#urls.py
url(r'^password_reset/', views.PasswordResetHandler.as_view()),
#views.py
class PasswordResetHandler(APIView):
def post(self, request, format=None):
try:
if '@' not in request.data['email']:
return Response({'status': 'Error', 'error': 'Invalid email address'}, status=404)
if User.objects.filter(email=request.data['email']):
#response = ...
# redirect to 'django_rest_passwordreset.urls'
return Response(some_response)
return Response({'status': 'Error', 'error': 'Email not found'}, status=404)
except:
return Response({'status': 'Error', 'error': 'Invalid data - fileds required: email'}, status=404)
我希望为无效电子邮件设置自定义例外:
api/password_reset
-> invalid_email
,email_not_found
api/password_reset/confirm
-> invalid reset token
,missing token
,invalid_new_password
如何以这种方式进行。我应该用其他方式吗?