Cookiecutter Django

repository·main·Indexed 11 days ago

https://github.com/cookiecutter/cookiecutter-django

A professional-grade project generator and Cookiecutter template for creating production-ready Django projects. Version 2026.8.9 supports Django 6.0 and Python 3.14, following 12-factor app principles with built-in configurations for security, testing (pytest), Celery, Sentry, and various cloud storage providers. It includes a flexible scaffolding process with options for REST APIs (DRF, Django Ninja), frontend pipelines, and Docker integration.

Tokens
21.6K
Snippets
68
Records
110
Agent score
95%

What's inside Cookiecutter Django

  1. What is Cookiecutter Django?

    main
    Cookiecutter Django is a project template designed to jumpstart production-ready Django projects. It provides a wide range of generation options to configure your project's architecture, including settings for databases, authentication, and various third-party integrations. It is powered by the cookiecutter tool.
  2. Core features of Cookiecutter Django

    main

    Cookiecutter Django provides a production-ready foundation with the following built-in features:

    • Django 6.0 & Python 3.14 support.
    • Testing: 100% starting test coverage with unittest or pytest support.
    • Frontend: Twitter Bootstrap v5.
    • Configuration: 12-Factor based settings using django-environ.
    • Security: Secure by default with SSL focus and custom user model included.
    • Authentication: Registration via django-allauth.
    • Email: Anymail integration (Mailgun default, switchable to Amazon SES).
    • Storage: Media storage support for Amazon S3, Google Cloud Storage, Azure Storage, or nginx.
    • Deployment: Docker support via docker-compose (using Traefik and LetsEncrypt), Procfile for Heroku, and instructions for PythonAnywhere.
    • DevOps: pre-commit integration for linting and issue identification.
  3. Understand email verification requirements in django-allauth

    main

    Cookiecutter Django uses django-allauth for authentication, which is configured by default to require email verification. This means the system will attempt to send a verification email in the following scenarios:

    • When a new user attempts to sign up.
    • When an unverified user attempts to log in.

    Because these actions trigger an email, ensure your email provider (like Mailgun) is fully configured and authorized to prevent application errors during the authentication flow.

  4. Understand the purpose of the django.contrib.sites directory

    main
    Cookiecutter Django includes a django.contrib.sites directory to automate the configuration of the sites.Site record. Instead of manually updating the site domain and name from the default example.com, the project uses a migration (0003_set_site_domain_and_name.py) to inject the values provided during the cookiecutter generation process: {{cookiecutter.domain_name}} for the domain and {{cookiecutter.project_name}} for the name.
  5. Optional integrations in Cookiecutter Django

    main

    During initial setup, you can enable several optional integrations to extend your project's capabilities:

    • Static Files: Serve via Amazon S3, Google Cloud Storage, Azure Storage, or Whitenoise.
    • Task Queues: Configuration for Celery and Flower (Flower is available in Docker setups).
    • Email Testing: Integration with Mailpit or Mailtrap Local for local development.
    • Error Logging: Integration with Sentry.
  6. Why the project layout differs from Two Scoops of Django

    main
    The project structure may not strictly follow the layout described in Two Scoops of Django 3.x. This is because Cookiecutter Django serves as a test bed for experimenting with new Django architectural ideas and concepts, which may result in deviations from the established patterns in the book.
  7. Configure HTTPS and Traefik

    main

    HTTPS is enabled by default using the Traefik reverse proxy, which automatically obtains and updates SSL certificates from Let's Encrypt.

    Requirements for Automatic HTTPS:

    1. DNS: Your DNS records must point to the server where Traefik is running.
    2. Allowed Hosts: If you are not using a subdomain of the project's default domain, you must add your staging/production IP address to the DJANGO_ALLOWED_HOSTS environment variable before deploying. Failure to do this will prevent access via HTTP/HTTPS.
    3. Admin Access: Access to the Django admin is configured to require HTTPS in production/live environments by default.
  8. Understand the Docker Compose service architecture

    main

    The docker-compose.production.yml file defines a multi-service stack. The services included depend on your project configuration:

    Core Services

    • django: The application running behind Gunicorn.
    • postgres: PostgreSQL database for relational data.
    • redis: Redis instance used for caching.
    • traefik: Reverse proxy that handles HTTPS by default.

    Celery Services (if use_celery=y was selected)

    • celeryworker: Runs Celery worker processes.
    • celerybeat: Runs the Celery beat scheduler.
    • flower: A monitoring tool served via Traefik over HTTPS on port 5555.
  9. How the template uses cloud storage buckets

    main

    When using a cloud provider for storage, the template uses a single bucket (or container) to hold both static files and user uploads, distinguished by prefixes:

    • static/: Contains the output of collectstatic. These files are intended to be publicly readable.
    • media/: Contains user uploads served via MEDIA_URL.

    Important Security Note: The template does not set per-object ACLs. Objects inherit the bucket's access rules. If you grant public read access to the entire bucket, your media/ files will also be publicly accessible. If your uploads are sensitive, follow the Keeping media private guidelines.

  10. Monitor Celery with Flower

    main

    If use_docker and use_celery were enabled during project initialization, a flower service is provided for real-time monitoring of the Celery task queue.

    • Access: http://localhost:5555
    • Authentication: Uses CELERY_FLOWER_USER and CELERY_FLOWER_PASSWORD environment variables defined in .envs/.local/.django (for local) or .envs/.production/.django (for production).
  11. Configure Webpack for production with Cloud Storage

    main

    If you use Webpack without Whitenoise, Webpack needs to know the static URL at build time. Because Docker does not read the .envs/.production/.django file during the build phase (it only looks for a .env file in the root), you must provide the static URL variables during the build command.

    Required Environment Variables:

    • AWS_STORAGE_BUCKET_NAME (or DJANGO_AWS_S3_CUSTOM_DOMAIN)
    • DJANGO_GCP_STORAGE_BUCKET_NAME (or DJANGO_GCP_STORAGE_BUCKET_NAME)
    • DJANGO_AZURE_CONTAINER_NAME (or DJANGO_AZURE_CONTAINER_NAME)

    Recommended Solution: Pass the variables directly to the build command:

    DJANGO_AWS_S3_CUSTOM_DOMAIN=example.com docker compose -f docker-compose.production.yml build