Django REST; JSON解析错误-期望属性名称用双引号引起来

Upon trying to test a CreateAPIView, I'm getting the following error for which I'm not sure how to resolve. I have explicitly defined my parsers and renderers as well in my settings. I'm not sure what property the error is referring to?

{'detail': 'JSON parse error - Expecting property name enclosed in double quotes: line 1 column 2 (char 1)'}

tests.py

class TestAPIRegisterView(TestCase):

    @classmethod
    def setUpTestData(cls):

        cls.register_form_data_2 = {
            "username": "User1",
            "password": "password",
            "verify_password": "passcode"
        }


    def test_register_user_password_match_fail(self):
        '''Verify that a 400 HTTP response is returned
        to the client informing it that the passwords entered
        in the register form don\'t match.'''

        http_response = self.client.post(
            reverse("register-user"),
            data=self.register_form_data_2,
            content_type="application/json"
        )

        print(http_response.json())

        self.assertEqual(http_response.status_code, 400)

views.py

class UserRegisterView(CreateAPIView):
    permission_classes = (permissions.AllowAny,)
    model = get_user_model()
    serializer_class = serializers.UserSerializer

serializers.py

class UserSerializer(serializers.ModelSerializer):
    password = serializers.CharField(write_only=True)
    verify_password = serializers.CharField(write_only=True)

    def create(self, validated_data):
        user = get_user_model().objects.create(
            username=validated_data['username'],
        )
        user.set_password(validated_data['password'])
        user.save()
        return user

    def validate(self, data):
        password = data["password"]
        verify_password = data["verify_password"]

        if password != verify_password:
            raise serializers.ValidationError(
                "Cannot verify your password. Try again."
            )
        return data

    class Meta:
        model = get_user_model()
        fields = ["username", "password", "verify_password"]

settings.py

REST_FRAMEWORK = {
    'DEFAULT_PARSER_CLASSES': (
        'rest_framework.parsers.JSONParser',
    ),
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
    )
}

评论