When starting a new Django project, one of the first things you'll encounter is deciding on the project structure. Django itself provides a default structure, but you might see seasoned developers opting for a slightly different approach. Today, we'll delve into two common Django project structures, discuss their pros and cons, and help you understand which one is generally preferred by professionals and why.
Let's take a look at the two structures we'll be comparing:
**Django-structure_1 (Nested Source Directory):**
erp_backend/
├── Dockerfile
├── docker-compose.yml
└── erp/
├── manage.py
├── accounts/
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations/
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── products/
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations/
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
└── erp/
├── __init__.py
├── asgi.py
├── settings.py
├── urls.py
└── wsgi.py
**Django-structure_2 (Default Flat Structure):**
erp_backend/
├── Dockerfile
├── docker-compose.yml
├── manage.py
├── accounts/
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations/
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── products/
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations/
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
└── erp/
├── __init__.py
├── asgi.py
├── settings.py
├── urls.py
└── wsgi.py
This is the structure you get straight out of the box when you run the django-admin startproject <project_name> . command (note the trailing dot which creates the project in the current directory).
Pros:
Cons:
erp_backend/ in this case) can become quite crowded. You'll have your Django apps (accounts, products), essential Django files (manage.py), and project-level configuration files (Dockerfile, docker-compose.yml) all residing at the same level. This can make it harder to navigate and distinguish between different types of project files.The first structure introduces an extra level of nesting by placing all your Django-related code within a dedicated source directory (named erp in this example, but often also named src).
Pros:
erp_backend/) becomes a clean space for project-level concerns like Docker configuration, CI/CD pipelines, documentation, and potentially other non-Django components (like a separate frontend application).erp.erp.settings (if erp is your source directory and also the inner project name).Cons:
erp_backend/) and then run django-admin startproject erp . inside a newly created erp/ directory (or src/).from erp.accounts.models import User). However, most professionals find this a worthwhile trade-off for the improved organization.