Django Rest Framework Simple JWT
Django rest framework with access and refresh token.
Django rest framework simple jwt doc here.
Django rest framework doc here.
Step 1: Preparation, Create Django Project, Initial Migration
create virtualenv: virtualenv venv
start virtualenv: venv/Scripts/activate
install Django in virtualenv: pip install django==3.0
install drf: pip install djangorestframework
install drf simplejwt: pip install djangorestframework_simplejwt
Create Django: django-admin startproject myproject
Go to myproject folder: cd myproject
Initial Migration: python manage.py migrate
Step 2: Create App and Add Django Rest Framework in Project Setting
Create apps: python manage.py startapp myapp
Add myapp and rest_framework into INSTALLED_APPS in myproject/settings.py
Step 3: Set Up Rest Framework Authentication
Add rest framework authentication code into settings.py
...
REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework_simplejwt.authentication.JWTAuthentication',
],
}
...
Step 4: Add APIView Class in Views.py
update myapp/views.py
Step 5: Setup URLs in myapp and myproject Folder
update myproject/urls.py
add myapp/urls.py
Step 6: Create Superuser
Create superuser: python manage.py createsuperuser
Type username, email password and retype password
Step 7: Run Server and Testing
Run Server: python manage.py runserver
Test using POSTMAN
Get Access and Refresh Token
method POST: http://127.0.0.1:8000/api/token/
Body with username and username that created before
Refresh Token
method POST: http://127.0.0.1:8000/api/token/refresh
Body with username, password and refresh token to get access token
Test get with Access Token
method GET: http://127.0.0.1:8000/hello