DEV Community

Pranav Bakare
Pranav Bakare

Posted on

Django | Project Structure | PART 1

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:

  1. 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.

  1. 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

  1. 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:

  1. 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.

  1. 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).

  1. 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.

  1. 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.

  1. 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.).

  1. 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:

  1. init.py

Makes the directory a package, allowing Python to recognize it as a module.

  1. 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.

  1. 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.

  1. models.py

Define the data models (tables) for your app. Django automatically creates tables based on these models when you run migrations.

  1. views.py

Contains the logic for handling web requests and returning responses. Views interact with models, templates, and other components.

  1. 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.

  1. urls.py

Define URL patterns specific to the app. These can be included in the main urls.py to keep URL configurations modular.

  1. migrations/

Stores migration files that keep track of changes made to the models. Migrations are used to update the database schema automatically.

  1. static/

Store static files like CSS, JavaScript, and images related to the app. They are served separately from your application code.

  1. templates/

Contains HTML templates for the app. You can create subfolders to organize templates better (e.g., templates/my_app/index.html).

  1. Other Important Folders

  2. 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.

  1. 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.

  1. 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.

Top comments (0)