How to Generate a New Django Project in Docker Using cookiecutter-django

By following this guide, you'll be equipped to create and manage Django projects efficiently within Docker, leveraging the power of cookiecutter-django

How to Generate a New Django Project in Docker Using cookiecutter-django

Introduction

In modern web development, containerization has become a crucial practice for ensuring consistency across different environments. Docker, a leading containerization platform, pairs perfectly with Django, a high-level Python web framework, to create robust and scalable web applications.

To streamline the process of setting up a Django project within Docker, cookiecutter-django offers a powerful and customizable project template. This guide will walk you through generating a new Django project using cookiecutter-django, explaining its benefits, the directory structure, command execution, and running the project in production. Additionally, we'll cover how to add a Django app named 'blog' with a model called Article.

Benefits of Using cookiecutter-django

  1. Rapid Project Setup: cookiecutter-django simplifies the creation of a new Django project with sensible defaults and best practices.
  2. Customization: Tailor your project with various options during setup, such as choosing the database, configuring Docker, and setting up CI/CD.
  3. Consistency: Ensures a consistent project structure and configuration across multiple projects.
  4. Pre-configured Docker Support: Seamlessly integrates Docker, reducing the complexity of setting up containerized environments.
  5. Security Best Practices: Incorporates security features like HTTPS, secure settings management, and more.

Generating a New Django Project

  1. Install Cookiecutter:
    pip install cookiecutter
    
  2. Generate a New Project:
    cookiecutter https://github.com/cookiecutter/cookiecutter-django
    

    Follow the prompts to configure your project. Make sure to select Docker support when prompted.

Directory Structure

After running cookiecutter, you'll get a project with the following structure:

my_project/
├── compose/
│   ├── local/
│   └── production/
├── config/
│   ├── settings/
│   └── urls.py
├── docs/
├── my_project/
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
├── Dockerfile
├── docker-compose.yml
├── requirements/
│   ├── base.txt
│   └── local.txt
├── README.md
└── tests/

Running Commands with manage.py

To execute Django management commands within Docker, use the following syntax:

docker-compose -f local.yml run --rm django python manage.py <command>

For example, to run migrations:

docker-compose -f local.yml run --rm django python manage.py migrate

To create a superuser:

docker-compose -f local.yml run --rm django python manage.py createsuperuser

Running the Project in Production

  1. Build the Docker Images:
    docker-compose -f production.yml build
    
  2. Run the Containers:
    docker-compose -f production.yml up -d
    
  3. Check the Logs:
    docker-compose -f production.yml logs
    

Adding a Django App: Blog

  1. Create the App:
    docker-compose -f local.yml run --rm django python manage.py startapp blog
    
  2. Add blog to INSTALLED_APPS: In config/settings/base.py, add 'blog' to the INSTALLED_APPS list.
  3. Define the Article Model: Edit blog/models.py to include the Article model:
    from django.db import models
    
    class Article(models.Model):
        title = models.CharField(max_length=200)
        content = models.TextField()
        published_date = models.DateTimeField(auto_now_add=True)
    
        def __str__(self):
            return self.title
    
  4. Create and Apply Migrations:
    docker-compose -f local.yml run --rm django python manage.py makemigrations blog
    docker-compose -f local.yml run --rm django python manage.py migrate
    

Summary

Using cookiecutter-django with Docker provides a robust, scalable, and customizable way to kickstart your Django projects. It ensures best practices, security, and consistency across projects, making development more efficient. By following the steps outlined in this guide, you can generate a new Django project, run it locally and in production, and extend it with additional apps like a blog.

More posts in Python