Django Project Directory Structure¶
A typical Django project is organized as follows:
myproject/ # Project root directory
├── manage.py # Django project management script
├── myproject/ # Project configuration directory
│ ├── __init__.py # Indicates this directory is a Python package
│ ├── settings.py # Project settings (configuration)
│ ├── urls.py # Project-wide URL routing
│ ├── wsgi.py # WSGI application entry point (for deployment)
│ └── asgi.py # ASGI application entry point (for async deployment)
├── app1/ # Application 1 directory (can have multiple apps)
│ ├── migrations/ # Database migration files
│ ├── __init__.py # Indicates this directory is a Python package
│ ├── admin.py # Admin interface configuration for this app
│ ├── apps.py # App-specific configuration
│ ├── models.py # Defines database models
│ ├── views.py # Application's view logic
│ ├── urls.py # App-specific URL routing
│ ├── static/ # App-specific static files (CSS, JS, images)
│ ├── templates/ # App-specific HTML templates
│ └── tests.py # Test cases for this app
├── app2/ # Application 2 directory (structure similar to app1)
└── static/ # Global static files directory (optional)
Key Elements in the Directory Structure¶
1. manage.py
¶
This is the main Django management script that you use to interact with your project. You can perform tasks such as running the development server, creating database migrations, and more. For example:
python manage.py runserver
Starts the local development server.
2. myproject/
(Project Configuration Directory)¶
This directory contains all the configuration files for your Django project.
-
__init__.py
: Marks the directory as a Python package. -
settings.py
: The central configuration file for your Django project. It contains settings for the database, static files, installed apps, middleware, templates, etc. It is common to customize this file for both development and production environments. -
urls.py
: The URL configuration for the entire project. This file maps URLs to views. It often includes URL routing from individual apps using Django'sinclude()
function. -
wsgi.py
: The entry point for WSGI-compatible web servers to serve your project. This file is used when deploying the project on a production server (e.g., Gunicorn, uWSGI). -
asgi.py
: The entry point for ASGI-compatible web servers, used for asynchronous communication such as WebSockets.
3. app1/
and app2/
(Application Directories)¶
Django projects can be made up of multiple apps. Each app is typically a module that provides a specific set of functionalities.
-
migrations/
: Stores migration files that track changes to the app's database schema over time. Every time you change your models, Django generates migration files that can be applied to update the database schema. -
admin.py
: Configures how your app's models are managed through Django's admin interface. -
apps.py
: Contains app-specific configuration. Django automatically creates this file when you start an app. -
models.py
: Defines the app's database models, which Django uses to create and manage the underlying database schema. -
views.py
: Contains the business logic for your app's views. Views process requests and return responses, typically by rendering a template or returning JSON data. -
urls.py
: Defines the URL patterns specific to this app. It's typically included in the project's mainurls.py
to handle app-specific routing. -
static/
: Stores the static files (CSS, JavaScript, images) specific to this app. Each app can have its ownstatic/
folder. -
templates/
: Contains the app's HTML template files. Django uses these templates to dynamically render content based on the data from views. -
tests.py
: Contains unit tests for the app. It's best practice to write test cases to ensure the app works as expected.
4. static/
(Global Static Files Directory)¶
This is an optional folder at the project level to store static files (e.g., CSS, JavaScript, images) that are shared across multiple apps. Django collects all static files during development or deployment, making them available at a single URL.
Key Configurations in settings.py
¶
Several important settings are related to the project structure, especially for managing static files, templates, and apps:
-
INSTALLED_APPS
: A list of all Django apps that are part of your project. Each app must be listed here for Django to recognize it. -
TEMPLATES
: Specifies where Django should look for template files. By default, Django looks for templates in thetemplates/
directory of each app. -
STATIC_URL
: Defines the URL prefix for serving static files, typically/static/
. -
STATICFILES_DIRS
: Specifies additional directories to look for static files beyond thestatic/
folders within apps. This is commonly used to define a globalstatic/
directory at the project level. -
MEDIA_URL
andMEDIA_ROOT
: Defines the URL and the directory for managing user-uploaded files (media).
Example of Using Static Files in Templates¶
In a Django template, you can load and use static files by using the {% load static %}
template tag:
{% load static %}
<link rel="stylesheet" href="{% static 'css/style.css' %}">
This example loads the static file style.css
from the static/css/
folder.
Summary¶
The Django project directory structure is designed to keep code organized and maintainable. It separates configuration, application logic, templates, static files, and tests into distinct directories. Each app in Django is a self-contained module that can be easily developed, tested, and reused in different projects.