Tutorial by Examples

Django REST Framework has some authentication methods already built in, one of them is Token based, so first thing to do is to tell our project we’re going to use rest framework’s authentication. Open settings.py file and add the highlighted line. INSTALLED_APPS = ( 'django.contrib.admin', ...
Token authentication functionality assigns a token to a user, so each time you use that token, the request object will have a user attribute that holds the user model information. Easy, isn’t it? We’ll create a new POST method to return the token for this user, as long as the request holds a correc...
Let’s create a new View that requires this authentication mechanism. We need to add these import lines: from rest_framework.authentication import TokenAuthentication from rest_framework.permissions import IsAuthenticated and then create the new View in the same views.py file class AuthView(AP...
If a curl would be run against this endpoint curl http://localhost:8000/auth/ op : {"detail": "Authentication credentials were not provided."}% would return a 401 error UNAUTHORIZED but in case we get a token before: curl -X POST -d "user=Pepe&password=aaaa&qu...

Page 1 of 1