2017-08-15 163 views
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

+1

能向我們展示完整的追溯?在哪裏發生錯誤? – devxplorer

+0

@devxplorer謝謝你的回答。我沒有錯誤,當我嘗試登錄一些用戶我有答案:'「無效的數據。期望一個字典,但得到請求。」'我發送請求像[this](http://take.ms/AfgHB)不能理解爲什麼。可以我做錯了 –

回答

1

您需要將request.data(而不僅僅是request)傳遞到串行器。

serializer = self.serializer_class(data=request.data) 
+0

wooww,謝謝!愚蠢,愚蠢的錯誤((( –

相關問題