如何在视图中验证电子邮件并重定向到依赖项URL

时间:2019-04-07 12:49:39

标签: django python-3.x django-views reset-password

我正在尝试为此自定义电子邮件例外:
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_emailemail_not_found
api/password_reset/confirm-> invalid reset tokenmissing tokeninvalid_new_password

如何以这种方式进行。我应该用其他方式吗?

0 个答案:

没有答案