Django: DRF SimpleJWT
LearningDRFHerokuPythonJWTDjangoAPI
Friday, June 14, 2024
This project demonstrates the basics of Django REST Framework (DRF) and the implementation of JSON Web Token (JWT) authentication using the Simple JWT library. The main objectives of this project were to learn how to set up JWT authentication and to explore the usage of the @api_view decorator to create custom APIs.
Features
- JWT Authentication using Simple JWT
- Custom APIs using @api_view
- Basic CRUD operations
- User authentication and authorization
Getting Started
Prerequisites
- Python 3.x
- Django
- Django REST Framework
- Simple JWT
Installation
- Clone the repository:
https://github.com/Muneeb1030/Django_rest_freamwork_simpleJWT.git cd Django_rest_freamwork_simpleJWT
- Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
- Install the dependencies:
pip install django djangorestframework djangorestframework-simplejwt
Set up the Django project:
django-admin startproject myproject cd myproject
- Create a new app:
python manage.py startapp myapp
- Add the new app and DRF to INSTALLED_APPS in settings.py:
INSTALLED_APPS = [ ... 'rest_framework', 'rest_framework_simplejwt', 'myapp', ]
- Configure DRF and Simple JWT in settings.py:
REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework_simplejwt.authentication.JWTAuthentication', ), } SIMPLE_JWT = { 'ACCESS_TOKEN_LIFETIME': timedelta(minutes=5), 'REFRESH_TOKEN_LIFETIME': timedelta(days=1), 'BLACKLIST_AFTER_ROTATION': True, 'ALGORITHM': 'HS256', 'VERIFYING_KEY': None, }
- Create and apply migrations:
python manage.py makemigrations python manage.py migrate
- Create a superuser:
python manage.py createsuperuser
Usage
The project includes basic user authentication and authorization functionality, as well as custom APIs using the @api_view decorator. Users can register, log in, and perform CRUD operations on protected endpoints.