Skip to content

miracle524/schedule_project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Django CRUD API with JWT Authentication

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.

Table of Contents

Features

  • Django REST Framework for API
  • JWT Authentication using djangorestframework-simplejwt
  • Swagger UI for API documentation
  • CRUD operations for weekly schedules

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd config
  2. Create and activate a virtual environment:

    python -m venv venv
    source venv/bin/activate  # Windows: venv\Scripts\activate
  3. Install the dependencies:

    pip install django djangorestframework djangorestframework-simplejwt drf-yasg

Setup

  1. Apply Migrations:

    python manage.py makemigrations
    python manage.py migrate
  2. Create a Superuser:

    python manage.py createsuperuser
  3. Run the Server:

    python manage.py runserver

Usage

Obtain JWT Token

curl -X POST http://localhost:8000/api/token/ -d '{"username": "user", "password": "pass"}'

Access Protected Endpoints

Use Bearer your_access_token in the Authorization header.

curl -X GET http://localhost:8000/api/schedules/ -H "Authorization: Bearer your_access_token"

API Endpoints

  • POST /api/token/: Get JWT token
  • GET /api/schedules/: List schedules
  • POST /api/schedules/: Create schedule
  • PUT /api/schedules/{id}/: Update schedule
  • DELETE /api/schedules/{id}/: Delete schedule

JWT Authentication

  1. Add JWT Settings in settings.py:

    INSTALLED_APPS = [
        'rest_framework',
        'rest_framework_simplejwt',
    ]
    REST_FRAMEWORK = {
        'DEFAULT_AUTHENTICATION_CLASSES': (
            'rest_framework_simplejwt.authentication.JWTAuthentication',
        ),
    }
  2. 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'),
    ]

Swagger Documentation

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/.

Releases

No releases published

Packages

No packages published

Languages