问题描述
我是 Django REST 框架的新手,被要求编写我们项目的令牌认证部分.需要注意的一件事是,由于将来我将不使用默认管理站点,因此我编写了登录、注销、注册功能,并通过 POSTMAN 测试功能.我现在要做的是让新用户注册,登录和注销.当用户登录时,我会发给他/她一个令牌.一切都以最简单的方式执行.
I am new to Django rest framework and was asked to write the token authentication part of our project. One thing to note is, as I would use not use the default admin site in future, I write login, logout, signup functions, and test the functionality by POSTMAN. What I want to do now is to let new user signup, login and logout. When a user log in, I issue him/her a token. Everything just perform in the easiest way.
但我仍然无法解决.我搜索了所有相关问题,但仍然无法解决我的问题.如果有人知道该怎么做,请帮助我!以下是详细内容.
But I still can't work it out. I searched all the related questions but still cannot solve my problem. If someone know how to do, please help me! Following is the details.
当我使用 GET 时,一切正常.但是当我使用 POST 时,我得到 MultiValueDictKeyError.我不知道为什么.
When I am using GET, everything works fine. But when I am using POST, I get MultiValueDictKeyError. I don't know why.
View.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, password=password)
user.save()
return Response({'detail': "Create user"})
@csrf_exempt
def login_view(request):
if request.method == 'POST':
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
user_logged_in.send(sender=user.__class__, request=request, user=user)
token = Token.objects.get_or_create(user=user)
return Response({
'detail': 'POST answer', 'token': token[0].key,
})
else:
return Response({'detail': "The username or password were incorrect.",
status: status.HTTP_404_NOT_FOUND})
@csrf_exempt
def logout_view(request):
if request.method == 'POST':
user = getattr(request, 'user', None)
if hasattr(user, 'is_authenticated') and not user.is_authenticated():
user = None
user_logged_out.send(sender=user.__class__, request=request, user=user)
if hasattr(request, 'user'):
from django.contrib.auth.models import AnonymousUser
request.user = AnonymousUser()
return Response({'detail': "You have logged out successfully."})
Test_app/Urls.py
from django.conf.urls import patterns, url
from rest_framework.urlpatterns import format_suffix_patterns
from test_app import views
urlpatterns = patterns('test_app.views',
url(r'^signup', views.create_user_view),
url(r'^login', views.login_view),
url(r'^logout', views.logout_view),
url(r'^auth', views.AuthView),
)
urlpatterns = format_suffix_patterns(urlpatterns)
Models.py(是的,我只在文件中放了两行)
from django.contrib.auth.models import User
from django.db import models
我还按照教程中的说明修改了 settings.py.
I also modified settings.py as tutorial said.
现在的问题是:
Request Method: POST
Request URL: http://127.0.0.1:8000/signup?username=haha&email=haha@gmail.com&password=okok
Django Version: 1.8.2
Exception Type: MultiValueDictKeyError
Exception Value:
"'username'"
Exception Location: /Users/wyq/PycharmProjects/env/lib/python2.7/site-packages/django/utils/datastructures.py in __getitem__, line 322
有人可以帮忙吗?非常感谢!
Can anyone help? Thank you very much!
推荐答案
MultiValueDictKeyError
发生在 QueryDict
当您尝试访问的键不在 QueryDict 中时.您的请求方法说 POST 但 url 方案建议您传递的参数将进入 request.GET
字典.
MultiValueDictKeyError
occurs in a QueryDict
when the key you are trying to access is not present in the QueryDict. Your request method says POST but the url scheme suggests that the parameters you are passing will go the request.GET
dict.
您需要通过表单或其他方式提交参数,以便在 request.POST
QueryDict
You need to submit the parameters via a form or something for them to be accessible in the request.POST
QueryDict
这篇关于使用 POST 时 Django 中的 MultiValueDictKeyError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!