所以我试图在使用django rest框架的应用程序中提供用户注册。我面临的问题是DRF基本上要求对请求进行身份验证
这是设置:
DEFAULT_AUTHENTICATION = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.OAuth2Authentication',
),
}
这是视图:
@permission_classes((AllowAny,))
@csrf_exempt
@api_view(['POST'])
def create_auth(request, format=None):
data = JSONParser().parse(request)
serialized = UserSerializer(data=data)
if serialized.is_valid():
user = User.objects.create_user(
serialized.init_data['email'],
serialized.init_data['username'],
serialized.init_data['password'],
)
user.groups = serialized.init_data['groups']
user.save()
serialized_user = UserSerializer(user)
return Response(serialized_user.data, status=status.HTTP_201_CREATED, headers={"Access-Control-Allow-Origin": "http://127.0.0.1:8000/"})
else:
return Response(serialized._errors, status=status.HTTP_400_BAD_REQUEST, headers={"Access-Control-Allow-Origin": "http://127.0.0.1:8000/"})
这是测试:
def test_user_register(self):
user_data = {'username': 'testusername',
'email': "[email protected]",
'groups': [1],
'password': 'testpassword',
}
resp = self.client.post("/auth/register", json.dumps(user_data), content_type="application/json")
print resp.content
self.assertEquals(resp.status_code, 200)
self.assertContains(resp, user_data["username"], 1, 201)
这是错误:
{"detail": "Authentication credentials were not provided."}
我究竟做错了什么?装饰器不应该允许未经身份验证的请求吗?甚至更好:通过REST API注册用户的正确方法是什么?
谢谢阿迪
在此处查看文档,其中显示:
... REST框架提供了一组其他装饰器,可以将其添加到视图中。这些必须位于@api_view装饰器之后(下方)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句