2015-06-25 758 views
2

我是Django rest框架的新手,並被要求編寫我們項目的令牌認證部分。有一點需要注意,因爲我將來不使用默認管理站點,所以我編寫登錄,註銷,註冊功能,並通過POSTMAN測試功能。我現在想要做的是讓新用戶註冊,登錄和註銷。當用戶登錄時,我向他/她發送令牌。一切都以最簡單的方式執行。使用POST時Django中的MultiValueDictKeyError

但我仍然無法解決它。我搜查了所有相關的問題,但仍然無法解決我的問題。如果有人知道該怎麼做,請幫助我! 以下是詳細信息。

當我使用GET時,一切正常。但是當我使用POST時,我得到MultiValueDictKeyError。我不知道爲什麼。

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作爲教程說。現在

的問題是:

Request Method: POST 
Request URL: http://127.0.0.1:8000/signup?username=haha&[email protected]&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 

誰能幫助?非常感謝你!

+0

這聽起來像你在請求發送'username'鍵多次訪問。 –

回答

0

MultiValueDictKeyError發生在QueryDict當您嘗試訪問的密鑰不存在於QueryDict中時。您的請求方法表示POST,但url方案表明您傳遞的參數將變爲request.GET字典。

,你需要通過形式或某事提交的參數爲他們是在request.POST的QueryDict

相關問題