python - MultiValueDictKeyError in Django when use POST -
i new django rest framework , asked write token authentication part of our project. 1 thing note is, use not use default admin site in future, write login, logout, signup functions, , test functionality postman. want let new user signup, login , logout. when user log in, issue him/her token. perform in easiest way.
but still can't work out. searched related questions still cannot solve problem. if know how do, please me! following details.
when using get, works fine. when using post, multivaluedictkeyerror. don't know why.
view.py
rest_framework.response import response django.contrib.auth import authenticate rest_framework.authtoken.models import token rest_framework import status django.contrib.auth.models import user rest_framework.authentication import tokenauthentication rest_framework.permissions import isauthenticated django.contrib.auth.signals import user_logged_in, user_logged_out rest_framework.decorators import api_view, authentication_classes, permission_classes 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 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 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') , not user.is_authenticated(): user = none user_logged_out.send(sender=user.__class__, request=request, user=user) if hasattr(request, 'user'): django.contrib.auth.models import anonymoususer request.user = anonymoususer() return response({'detail': "you have logged out successfully."})
test_app/urls.py
django.conf.urls import patterns, url rest_framework.urlpatterns import format_suffix_patterns 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(yes, put 2 lines in file)
django.contrib.auth.models import user django.db import models
i modified settings.py tutorial said.
the problem is:
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 help? thank much!
multivaluedictkeyerror
occurs in querydict
when key trying access not present in querydict. request method says post url scheme suggests parameters passing go request.get
dict.
you need submit parameters via form or them accessible in request.post
querydict
Comments
Post a Comment