如何重新发送验证邮件 django rest 框架?

How to resend verification email django restframework?

我可以在注册用户时发送一封验证邮件,以便激活帐户。但是如何在 API 上重新发送另一封验证电子邮件?在这里,我正在使用令牌进行激活 link,当用户打开 link 时,它会获取令牌并验证用户。 但如何重新发送验证邮件?

class RegisterUser(APIView):
    serialzer_class = RegisterSerialzer
    def post(self, request):
        user = request.data
        serializer = self.serialzer_class(data = user)
        serializer.is_valid(raise_exception = True)     
        serializer.save()
        user_data = serializer.data
        # user = User.objects.get(username=serializer.data['username'])
        # print(user.id)
        # token = Token.objects.create(user=user)
        user = User.objects.get(email = user_data['email'])
        token = RefreshToken.for_user(user).access_token
        current_site = get_current_site(request).domain
        relativeLink = reverse('email-verify')
        
        absurl = 'http://'+current_site+relativeLink+"?token="+str(token)
        email_body = 'Hi '+ user.username + 'user the link below to verify your email \n' + absurl

        data = {'email_body':email_body,'to_email':user.email,
                'email_subject':'Verify your email'}
        Util.send_email(data)

class VerifyEmail(APIView):
    def get(self, request):
        token = request.GET.get('token')
        try:
            payload = jwt.decode(token, settings.SECRET_KEY)
            user = User.objects.get(id=payload['user_id'])
            user.is_verified = True
            # user.is_authenticated = True
            user.is_active = True
            # if not user.is_verified:
            user.save()
            return Response({'email':'successfuly activated'}, status=status.HTTP_200_OK)
        # except jwt.ExpiredSignatureError as identifier:
        except jwt.ExpiredSignatureError:
            return Response({'error':'Activation Expired expired'}, status=status.HTTP_400_BAD_REQUEST)
        # except jwt.exceptions.DecodeError as identifier:
        except jwt.exceptions.DecodeError:
            return Response({'error':'invalid token'}, status=status.HTTP_400_BAD_REQUEST)

我的解决方法是从用户或前端请求电子邮件,然后查询用户以获取电子邮件地址并将其发送到电子邮件。

class ResendVerifyEmail(APIView):
    serializer_class = RegisterSerialzer
    def post(self, request):
        data = request.data
        # email = data.get('email')
        email = data['email']
        print(email)
        try:
            user = User.objects.get(email=email)
       
            print('hello')
            if user.is_verified:
                return Response({'msg':'User is already verified'})
            print (user.username)
            token = RefreshToken.for_user(user).access_token
            current_site= get_current_site(request).domain
            relativeLink = reverse('email-verify')
            
            absurl = 'http://'+current_site+relativeLink+"?token="+str(token)
            email_body = 'Hi '+ user.username + ' this is the resent link to verify your email \n' + absurl

            data = {'email_body':email_body,'to_email':user.email,
                    'email_subject':'Verify your email'}
            Util.send_email(data)
            return Response({'msg':'The verification email has been sent'}, status=status.HTTP_201_CREATED)
        except User.DoesNotExist:
            return Response({'msg':'No such user, register first'})