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
- Rapid Project Setup: cookiecutter-django simplifies the creation of a new Django project with sensible defaults and best practices.
- Customization: Tailor your project with various options during setup, such as choosing the database, configuring Docker, and setting up CI/CD.
- Consistency: Ensures a consistent project structure and configuration across multiple projects.
- Pre-configured Docker Support: Seamlessly integrates Docker, reducing the complexity of setting up containerized environments.
- Security Best Practices: Incorporates security features like HTTPS, secure settings management, and more.
Generating a New Django Project
- Install Cookiecutter:
pip install cookiecutter
- 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
- Build the Docker Images:
docker-compose -f production.yml build
- Run the Containers:
docker-compose -f production.yml up -d
- Check the Logs:
docker-compose -f production.yml logs
Adding a Django App: Blog
- Create the App:
docker-compose -f local.yml run --rm django python manage.py startapp blog
- Add blog to INSTALLED_APPS:
In
config/settings/base.py
, add'blog'
to theINSTALLED_APPS
list. - Define the
Article
Model: Editblog/models.py
to include theArticle
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
- 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.