0
對不起,我的英語。我學習Django的休息,我想創建令牌授權。我做到了通過教程,但我有錯誤,當數據發送到服務器AuthTokenSerializer。無效數據。期待一本字典,但得到了請求。 Django
{
"non_field_errors": [
"Invalid data. Expected a dictionary, but got Request."
]
}
我的設置文件:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'users',
]
..
..
REST_FRAMEWORK = {
'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAuthenticated',),
'PAGE_SIZE': 10,
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework.authentication.TokenAuthentication',
'rest_framework.authentication.BasicAuthentication',
'rest_framework.authentication.SessionAuthentication',
)
}
我的網址
urlpatterns = [
url(r'^authtoken', views.ObtainAuthToken.as_view()),
]
我看來
class ObtainAuthToken(APIView):
permission_classes = (permissions.AllowAny,)
serializer_class = AuthTokenSerializer
def post(self, request):
serializer = self.serializer_class(data=request)
serializer.is_valid(raise_exception=True)
user = serializer.validated_data['user']
token, created = Token.objects.get_or_create(user=user)
return Response({'token': token.key}, status.HTTP_201_CREATED)
我不明白爲什麼我有呃ror
能向我們展示完整的追溯?在哪裏發生錯誤? – devxplorer
@devxplorer謝謝你的回答。我沒有錯誤,當我嘗試登錄一些用戶我有答案:'「無效的數據。期望一個字典,但得到請求。」'我發送請求像[this](http://take.ms/AfgHB)不能理解爲什麼。可以我做錯了 –