Django Rest Framework - How to create token based authentication

Go to the python shell under your django project

python manage.py shell

 Once you are inside shell, complete token creation by using following commands

from django.contrib.auth.models import User
user=User.objects.create_user('username', password='password')
user.is_superuser=False
user.is_staff=False
user.save()
from rest_framework.authtoken.models import Token
token = Token.objects.create(user=user)
print(token.key)

In case you want to enable token for already existing user, first get the user by it's username

user=User.objects.get(username='old_user')

and then create token

from rest_framework.authtoken.models import Token
token = Token.objects.create(user=user)
print(token.key)