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.
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(Yes, I only put two lines in the file)
from django.contrib.auth.models import User
from django.db import models
I also modified settings.py as tutorial said.
The problem now 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 anyone help? Thank you very much!