1

我正在尝试将PUT一些数据转换为UserAccount模型,以便在不使用 Web 模板(对于移动应用程序)的情况下重置密码。这里的模型

class UserAccount(AbstractBaseUser, PermissionsMixin):
    email = models.EmailField(max_length=255, unique=True)
    name = models.CharField(max_length=255)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_password_reset_confirmed = models.BooleanField(default=False)
    password_reset_token = models.CharField(max_length=255, default="")

    objects = UserAccountManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['name']

    def get_full_name(self):
        return self.name
    
    def get_short_name(self):
        return self.name
    
    def __str__(self):
        return self.email

我发送的数据不会保存。但返回Success response 204. 这是我的serializer.py

class UserAccountPasswordResetConfirmationSerializer(serializers.ModelSerializer):

    class Meta:
        model = UserAccount
        fields = ['is_password_reset_confirmed', 'password_reset_token']

视图.py

class UserPasswordResetConfirmView(APIView):

    def get(self, request, uid, token, format=None):
        payload = {
            'is_confirmed': True,
            'token': token
        }

        url = 'http://localhost:8000/password/reset/confirmed/{}/'.format(smart_str(urlsafe_base64_decode(uid)))
        response = requests.put(url, data=payload)

        if response.status_code == 204:
            return Response(response.content)
        else:
            return Response({"Error": "Failed to Confirm"})


class UserPasswordResetView(APIView):

    def get_object(self, pk):
        try:
            return UserAccount.objects.get(pk=pk)
        except UserAccount.DoesNotExist:
            raise Response(status=status.HTTP_404_NOT_FOUND)

    def put(self, request, pk):
        user = self.get_object(pk)
        # print("Data", request.data)
        serializer = UserAccountPasswordResetConfirmationSerializer(user, data=request.data)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_204_NO_CONTENT)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

我检查了数据是否UserPasswordResetViewUserPasswordResetConfirmViewusing printstatements(^.^') 传递,这没有问题。

 Data <QueryDict: {'is_confirmed': ['True'], 'token': ['5m0-60f758fc23628d143158']}>

但是response.content返回 "" 并且数据不会更新。

除了该控制台表明,

[30/Nov/2020 14:56:05] "PUT /password/reset/confirmed/1/ HTTP/1.1" 204 63
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 64019)
Traceback (most recent call last):
  File "C:\Program Files\Python37\lib\socketserver.py", line 650, in process_request_thread
    self.finish_request(request, client_address)
  File "C:\Program Files\Python37\lib\socketserver.py", line 360, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Program Files\Python37\lib\socketserver.py", line 720, in __init__
    self.handle()
  File "C:\Users\Ozunu\AppData\Roaming\Python\Python37\site-packages\django\core\servers\basehttp.py", line 174, in handle
    self.handle_one_request()
  File "C:\Users\Ozunu\AppData\Roaming\Python\Python37\site-packages\django\core\servers\basehttp.py", line 182, in handle_one_request
    self.raw_requestline = self.rfile.readline(65537)
  File "C:\Program Files\Python37\lib\socket.py", line 589, in readinto
    return self._sock.recv_into(b)
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
----------------------------------------
[30/Nov/2020 14:56:05] "GET /password/reset/confirm/MQ/5m0-60f758fc23628d143158/ HTTP/1.1" 200 5540

顺便说一句,我正在使用MongoDBand djoser。为什么会发生这种情况以及任何帮助将不胜感激。


编辑:

我已经测试了 的端点,UserPasswordResetView(APIView)它对相关记录进行了更新。


编辑

我能够通过转换解决不保存的问题

<QueryDict: {'is_confirmed': ['True'], 'token': ['5m0-60f758fc23628d143158']}>

到字典

{'is_confirmed': 'True', 'token': '5m0-60f758fc23628d143158'}

但无法解决ConnectionAbortedError:错误。

4

0 回答 0