Skip to content

A comprehensive and easy-to-use starting point for your new API with Django and DRF

License

Notifications You must be signed in to change notification settings

wilfredinni/django-starter-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Django starter template

A comprehensive and easy-to-use starting point for your new API with Django and DRF.

Test Status CodeQL Status MIT License

Table of Contents

Key Features

Core Platform

  • πŸš€ Django 5+ with full feature set
  • πŸ› οΈ Django Rest Framework for API development
  • πŸ“– API documentation (drf-spectacular + Swagger)
  • πŸ’Ώ PostgreSQL database
  • πŸ“¦ Redis caching
  • ⏳ Celery task management

Development Tools & Features

  • πŸ§ͺ Pytest testing suite with code Coverage
  • ⚑ Jupyter Notebooks integration
  • 🐞 Django Debug Toolbar
  • πŸ”§ Code quality tools (Black, Flake8)
  • πŸ‘¨β€πŸ’» VS Code with Dev Containers
  • πŸ”’ Knox authentication system
  • πŸ”½ Advanced filtering capabilities

Quick Start

Prerequisites

  • πŸ’» VS Code
  • πŸ‹ Docker
  • 🐳 Docker Compose

Setup Steps

  1. Use GitHub's template feature (recommended) or clone repository
  2. Open in VS Code
  3. Check Todo Tree in the sidebar for setup guidance
  4. Run CTL/CMD + Shift + p and select Reopen in container
  5. Create superuser: python manage.py createsuperuser
  6. Start server: python manage.py runserver

Development Guide

Authentication

The template uses Knox for token-based authentication:

Available Endpoints

  • POST /auth/create/ - Create a new user
  • POST /auth/login/ - Log in and receive token
  • POST /auth/logout/ - Invalidate current token
  • POST /auth/logoutall/ - Invalidate all tokens
  • GET/PUT/PATCH /auth/profile/ - Manage user profile

Rate Limiting

  • Login attempts: 5 per minute
  • User operations: 1000 per day
  • Anonymous operations: 100 per day

API Documentation

  • Swagger UI available at /api/schema/swagger-ui/
  • Automatic schema generation with drf-spectacular
  • Includes example requests and responses
  • Documents authentication requirements

Testing

  • Tests are organized by app in tests/ directories
  • Uses pytest fixtures for common testing scenarios
  • Includes comprehensive test coverage for authentication
  • Includes examples of API endpoint testing

Task Processing

Tasks with retry

The template includes a base task class with retry capabilities. You can use it to create tasks that automatically retry on failure.

from apps.core.tasks import BaseTaskWithRetry

@shared_task(base=BaseTaskWithRetry)
def my_task():
    # Will retry 3 times with exponential backoff
    pass

Scheduled Tasks

Scheduled tasks are managed with Celery Beat. You can define your periodic tasks in the tasks.py file of your app and configure them from the Django admin interface.

from apps.core.tasks import BaseTask
@shared_task(base=BaseTask)
def my_periodic_task():
    # This task can be scheduled to run at regular intervals
    # from the Django admin interface
    pass

Useful Commands

This section provides a list of useful commands to help you manage and develop your Django project efficiently.

Celery Tasks

  • poetry run worker: to start a new Celery worker.
  • poetry run beat: to start your periodic tasks.

Test commands:

  • pytest to run the tests.
  • pytest --cov to run the tests with coverage.
  • pytest --cov --cov-report=html to run the tests with coverage and generate a HTML report.

You can also use

  • poetry run server instead of python manage.py runserver
  • poetry run makemigrations instead of python manage.py makemigrations
  • poetry run migrate instead of python manage.py migrate
  • poetry run create_dev_env to create a development .env file
  • poetry run seed to seed your database with sample data

Database Seeding

The template includes a powerful seeding command to populate your database with sample data for development and testing:

# Basic seeding with default options (creates 10 users)
python manage.py seed

# Create specific number of users
python manage.py seed --users 20

# Create a superuser (admin@admin.com:admin)
python manage.py seed --superuser

# Clean existing data before seeding
python manage.py seed --clean

# Combine options
python manage.py seed --users 50 --superuser --clean

Environment Setup

  • Development: .env file created automatically
  • Production: See .env.example for required variables

Security Features

  • Email-based authentication
  • Token-based authorization
  • Rate limiting and throttling
  • CORS configuration
  • Debug mode control
  • Secure password hashing

Project Structure

β”œβ”€β”€ .devcontainer/              # Dev container config
β”‚
β”œβ”€β”€ .github/                    # GitHub CI/CD workflows
β”‚
β”œβ”€β”€ .vscode/                    # VS Code settings
β”‚
β”œβ”€β”€ apps/
β”‚   β”œβ”€β”€ core/                   # Core functionality
β”‚   β”‚   β”œβ”€β”€ management/         # Custom management commands
β”‚   β”‚   β”œβ”€β”€ tests/              # Core app tests
β”‚   β”‚   β”œβ”€β”€ schema.py           # API schema definitions
β”‚   β”‚   β”œβ”€β”€ tasks.py            # Celery base tasks
β”‚   β”‚
β”‚   └── users/                  # User Management and Authentication app
β”‚       β”œβ”€β”€ tests/              # User app tests
β”‚       β”œβ”€β”€ managers.py         # User model managers
β”‚       β”œβ”€β”€ models.py           # User model definition
β”‚       β”œβ”€β”€ schema.py           # User API schemas
β”‚       β”œβ”€β”€ throttles.py        # Rate limiting
β”‚       β”œβ”€β”€ views.py            # User API views for authentication
β”‚
β”œβ”€β”€ conf/                       # Project configuration
β”‚   β”œβ”€β”€ settings.py             # Main settings file
β”‚   β”œβ”€β”€ test_settings.py        # Test-specific settings
β”‚   β”œβ”€β”€ celery.py               # Celery configuration
β”‚
β”œβ”€β”€ logs/                       # Application Info and Error logs
β”‚
β”œβ”€β”€ scripts/                    # Utility scripts
β”‚
β”œβ”€β”€ .env.example                # Example environment variables
β”œβ”€β”€ .flake8                     # Flake8 configuration
β”œβ”€β”€ .gitignore                  # Git ignore file
β”œβ”€β”€ manage.py                   # Django management script
β”œβ”€β”€ notebook.ipynb              # Jupyter Notebook
β”œβ”€β”€ pyproject.toml              # Project dependencies
β”œβ”€β”€ pytest.ini                  # Testing configuration

Recommended Packages

Todo & Roadmap

  • Index Page with a link to the Django admin
  • OpenAPI 3 schema generation and Swagger
  • CI with Github Actions
  • Data seeding
  • Production Docker file
  • Production Docker compose file
  • CD with Github Actions