Django rest-framework JWT authentication

PythonServer Side ProgrammingProgramming

If you ever worked with Django REST framework, then you surely know about JWT authentication. JWT authentication is used for token authentication and it is really a popular method for authentication in Django. JWT stand for JSON Web Token. Let's see how to work with it.

First, install a package −

pip install djangorestframework-simplejwt
pip install djangorestframework

We are not going to need an App for this, we will just do a basic setup for Django REST framework frontend and authentication backend.


In, add the following −


Here, we added the rest_framework and JWT auth framework as an app and for authentication backend, we added JWT auth.

In project's

from django.urls import path
from rest_framework_simplejwt.views import (
urlpatterns = [
   path('api/token/', TokenObtainPairView.as_view(), name='t
   path('api/token/refresh/', TokenRefreshView.as_view(), na

Here, we added two extra urls, one is for generating token in with username and password and the other is for authentication using generated token.


Published on 25-Aug-2021 13:09:50