python - AuthTokenSerializer. Invalid data. Expected a dictionary, but got Request. Django -
sorry english. have learning django rest , want create token authorization. did tutorial, have error, when send data server
{ "non_field_errors": [ "invalid data. expected dictionary, got request." ] } my setting file:
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', ) } my url
urlpatterns = [ url(r'^authtoken', views.obtainauthtoken.as_view()), ] my 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) i dont understand why have error
you need pass request.data, not request, serializer.
serializer = self.serializer_class(data=request.data)
Comments
Post a Comment