小编Chr*_*661的帖子

使用 POST 时 Django 中的 MultiValueDictKeyError

我是 Django Rest 框架的新手,被要求编写我们项目的令牌身份验证部分。需要注意的一件事是,因为我将来不会使用默认的管理站点,所以我编写了登录、注销、注册功能,并通过 POSTMAN 测试了功能。我现在想做的是让新用户注册、登录和注销。当用户登录时,我向他/她颁发一个令牌。一切都以最简单的方式进行。

但我还是搞不出来。我搜索了所有相关问题,但仍然无法解决我的问题。如果有人知道该怎么做,请帮助我!以下是详细内容。

当我使用 GET 时,一切正常。但是当我使用 POST 时,我收到 MultiValueDictKeyError。我不知道为什么。

查看.py

    from rest_framework.response import Response
    from django.contrib.auth import authenticate
    from rest_framework.authtoken.models import Token
    from rest_framework import status
    from django.contrib.auth.models import User
    from rest_framework.authentication import TokenAuthentication
    from rest_framework.permissions import IsAuthenticated
    from django.contrib.auth.signals import user_logged_in, user_logged_out
    from rest_framework.decorators import api_view, authentication_classes, permission_classes
    from django.views.decorators.csrf import csrf_exempt, requires_csrf_token
    @csrf_exempt
    @requires_csrf_token
    @api_view(['POST'])
    def create_user_view(request):
        if request.method == 'POST':
            username = request.POST['username']
            email = request.POST['email']
            password = request.POST['password']
            user = User.objects.create_user(username=username, email=email, …
Run Code Online (Sandbox Code Playgroud)

python token django-rest-framework postman

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

django-rest-framework ×1

postman ×1

python ×1

token ×1