Django's default project structure organizes files and folders to help developers manage code efficiently. Here's a breakdown of the typical file structure when you create a new Django project:
- Project Root Folder
When you start a new Django project, it generates a root folder named after the project (e.g., my_project/). This folder contains the overall project files.
- manage.py
This script acts as a command-line utility for interacting with your Django project. You can run commands like starting the development server, making migrations, creating superusers, and more.
Usage:
python manage.py runserver
python manage.py migrate
python manage.py createsuperuser
- Project Subdirectory (my_project/)
A subdirectory within the project root that contains the core settings and configurations for your Django project.
It usually has the same name as your project.
Files in the Project Subdirectory:
- init.py
An empty file that tells Python to treat this directory as a package. It allows you to import other modules within the project.
- settings.py
Contains all the configuration settings for your Django project, such as database connections, installed apps, middleware, templates, static files, and more.
You can customize settings for different environments (development, staging, production) by splitting this file into multiple settings files (e.g., settings_dev.py, settings_prod.py).
- urls.py
The central URL configuration file. It maps URLs to their respective views. You can include other URL configurations here by importing them, making it easier to manage large projects.
- wsgi.py
Stands for "Web Server Gateway Interface." This file is used to deploy your project to a production web server. It acts as the entry point for the web server to serve your Django application.
- asgi.py
Stands for "Asynchronous Server Gateway Interface." It’s similar to wsgi.py, but it's used for handling asynchronous web requests (useful for WebSockets, HTTP2, etc.).
- Application Folders (my_app/)
Applications are reusable modules within a Django project. Each app typically handles a specific part of your project's functionality (e.g., users, blog, shop).
You can create new apps using:
python manage.py startapp my_app
Files in the Application Folder:
- init.py
Makes the directory a package, allowing Python to recognize it as a module.
- admin.py
Contains code to customize the Django Admin interface for this app. You can define how models are presented and managed in the admin panel.
- apps.py
Stores configuration for the app, like the app’s name and any custom behavior. It’s automatically created when the app is generated.
- models.py
Define the data models (tables) for your app. Django automatically creates tables based on these models when you run migrations.
- views.py
Contains the logic for handling web requests and returning responses. Views interact with models, templates, and other components.
- tests.py
Used to write unit tests for the app. It’s a good practice to write tests to ensure that your application works as expected.
- urls.py
Define URL patterns specific to the app. These can be included in the main urls.py to keep URL configurations modular.
- migrations/
Stores migration files that keep track of changes made to the models. Migrations are used to update the database schema automatically.
- static/
Store static files like CSS, JavaScript, and images related to the app. They are served separately from your application code.
- templates/
Contains HTML templates for the app. You can create subfolders to organize templates better (e.g., templates/my_app/index.html).
Other Important Folders
static/ (Global Static Folder)
Store static files (CSS, JavaScript, images) that are not tied to a specific app. During deployment, static files are collected from each app’s static/ directory and served to the client.
- templates/ (Global Templates Folder)
A central place to store templates that are used across the entire project. It’s beneficial for shared layouts and components.
- media/
Store user-uploaded files. Make sure to configure your settings.py to serve media files correctly.
Example Project Structure:
my_project/
│
├── manage.py
├── my_project/ # Project subdirectory
│ ├── init.py
│ ├── settings.py
│ ├── urls.py
│ ├── wsgi.py
│ └── asgi.py
│
├── my_app/ # An example application
│ ├── init.py
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── views.py
│ ├── urls.py
│ ├── tests.py
│ ├── migrations/
│ ├── static/
│ └── templates/
│
├── static/ # Global static files
│ └── css/
│ └── js/
│
└── templates/ # Global templates
└── base.html
└── index.html
This structure helps keep your project organized, scalable, and easy to maintain.