This project demonstrates a Django REST API for managing weekly schedules with JWT (JSON Web Token) authentication. The API uses djangorestframework-simplejwt
for JWT and Swagger for interactive documentation.
- Django REST Framework for API
- JWT Authentication using
djangorestframework-simplejwt
- Swagger UI for API documentation
- CRUD operations for weekly schedules
-
Clone the repository:
git clone <repository-url> cd config
-
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # Windows: venv\Scripts\activate
-
Install the dependencies:
pip install django djangorestframework djangorestframework-simplejwt drf-yasg
-
Apply Migrations:
python manage.py makemigrations python manage.py migrate
-
Create a Superuser:
python manage.py createsuperuser
-
Run the Server:
python manage.py runserver
curl -X POST http://localhost:8000/api/token/ -d '{"username": "user", "password": "pass"}'
Use Bearer your_access_token
in the Authorization
header.
curl -X GET http://localhost:8000/api/schedules/ -H "Authorization: Bearer your_access_token"
POST /api/token/
: Get JWT tokenGET /api/schedules/
: List schedulesPOST /api/schedules/
: Create schedulePUT /api/schedules/{id}/
: Update scheduleDELETE /api/schedules/{id}/
: Delete schedule
-
Add JWT Settings in
settings.py
:INSTALLED_APPS = [ 'rest_framework', 'rest_framework_simplejwt', ] REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework_simplejwt.authentication.JWTAuthentication', ), }
-
Add Token URLs in
urls.py
:from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView urlpatterns += [ path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'), path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'), ]
Add Swagger URLs for API documentation:
from drf_yasg.views import get_schema_view
from drf_yasg import openapi
schema_view = get_schema_view(
openapi.Info(
title="Schedule API",
default_version='v1',
),
public=True,
)
urlpatterns += [
path('swagger/', schema_view.with_ui('swagger'), name='schema-swagger-ui'),
]
Access Swagger at http://localhost:8000/swagger/
.