AuthTokenSerializer。无效数据。期待一本字典,但得到了请求。 Django的

时间:2017-08-15 13:17:20

标签: python django

抱歉我的英文。我已经学习了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)

我不明白为什么我有错误

1 个答案:

答案 0 :(得分:3)

您需要将min而不仅仅是import re def get_frame_number(file): match = re.match(r'[\w\d]+\.(\d+)\.jpg', file) if match: return int(match.group(1)) else: return float('nan') >>> max(files, key=get_frame_number) 'foo.0005.jpg' >>> min(files, key=get_frame_number) 'foo.0001.jpg' 传递给序列化程序。

request.data