我制作了一个注册api,我用它返回了身份验证凭证

时间:2019-05-10 02:52:17

标签: python django authentication django-rest-framework

我有此代码要注册,但是当我注册时返回此代码。
{detail: "authentication credentials were not provided"} 这是我的代码,需要帮助来处理我的整个应用程序

class RegisterApi(GenericAPIView):
    serializer_class = RegisterSerializer
    queryset = User.objects.all()

    def post(self, request, format=None, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.save(request=self.request)
        return Response({
            "user": UserSerializer(user, context=self.get_serializer_context()).data,
            "token": AuthToken.objects.create(user)
        })

,这里是两个序列化器类

# User Serializer


class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ("id", "username", "email")


# Register Serializer

class RegisterSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ("id", "username", "email")
        extra_kwargs = {"password": {"write_only": True}}

    def create(self, data):
        user = User.objects.create(
            data["username"], data["email"]
        )
        user.set_password(data["password"])
        user.save()
        return user

1 个答案:

答案 0 :(得分:0)

创建一个超级用户并登录到admin部分,然后调用您的api。我认为这是工作。

raw_material_list = [
     {
       "id": "44",
       "created_date": "2019-04-19 12:01:13",
       "activeFlag": "true",
       "productType": "purchase",
       "productName": "suraksha",
     },
     {
       "id": "43",
       "created_date": "2019-04-19 11:59:59",
       "activeFlag": "true",
       "productType": "purchase",
       "productName": "vajra",
     }
   ];