Practical Python Programming: An Introduction to Django Project Structure

Django is one of the most mature web frameworks in Python, featuring a complete set of functionalities such as built-in ORM, template engine, permission system, and backend management, making it very suitable for building medium to large web applications.

One of the most confusing questions for beginners when creating a Django project is: “What are these folders for?”

This article will help you thoroughly understand the structure of a Django project and master how a Django project should be organized and run.

1. Creating a Django Project

Assuming you have already installed Django:

pip install django

Create a project:

django-admin startproject mysite

The structure is as follows:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py

2. Explanation of the Project Root Directory Structure

Below is an explanation of the default directories in a Django project.

1. manage.py — Project Management Command Entry

The commands you use most frequently during development are almost all run from here:

python manage.py runserver
python manage.py migrate
python manage.py createsuperuser
python manage.py startapp app1

Functions:

  • • Provides an entry point for Django command-line tools
  • • Automatically configures Django environment variables

2. Main Project Directory (named after the project)

The second layer, <span>mysite/</span>, is the actual project configuration directory, which contains project-level configuration files.

The structure is as follows:

mysite/
    __init__.py
    settings.py
    urls.py
    asgi.py
    wsgi.py

Let’s introduce them one by one:

2.1 settings.py — Core Configuration File of the Project

<span>settings.py</span> is the “brain” of the Django project, containing all global configurations, such as:

  • • INSTALLED_APPS (installed components)
  • • DATABASES (database configuration)
  • • MIDDLEWARE (middleware)
  • • TEMPLATES (template settings)
  • • STATICFILES (static file paths)
  • • DEBUG switch

This is the file you will modify the most.

2.2 urls.py — Project URL Routing Entry

Defines the page paths of the website, determining which app should handle a specific URL.

Example:

from django.urls import path, include

urlpatterns = [
    path('', include('app1.urls')),
    path('admin/', admin.site.urls),
]

Suggestion: Each app should have its own urls.py, included through the main project..

2.3 wsgi.py — Used for Deploying WSGI Servers

Used for deploying Python web services (such as gunicorn, uWSGI).

Accessing Django in a production environment is done through it.

2.4 asgi.py — Asynchronous Deployment Entry

ASGI (Asynchronous Gateway Interface) supports:

  • • WebSocket
  • • Asynchronous views

This is provided by default in new Django projects and is very important.

3. Structure of a Django Application (app)

When you create a new app:

python manage.py startapp blog

It will generate the following structure:

blog/
    migrations/
        __init__.py
    __init__.py
    admin.py
    apps.py
    models.py
    tests.py
    views.py

Let’s explain each one:

1. models.py — Data Model (ORM)

Used to define the database table structure.

Example:

class Article(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()

2. views.py — View Layer (Business Logic)

Handles requests and returns responses.

from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello Django")

3. urls.py (needs to be created manually) — App Routing

It is recommended to add a <span>urls.py</span> for each app:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home),
]

Include it in the main project:

path('blog/', include('blog.urls'))

4. admin.py — Django Backend Management Configuration

Allows data models to be managed in the backend:

from django.contrib import admin
from .models import Article

admin.site.register(Article)

Backend address:

/admin/

5. apps.py — App Configuration Information

Generally, no modification is needed.

6. migrations/ — Database Migration Records

Execute:

python manage.py makemigrations
python manage.py migrate

All database structure changes will be saved as scripts here.

7. tests.py — Unit Testing

Used for writing automated tests.

4. Recommended Django Project Structure (Commonly Used in Enterprises)

As projects grow larger, the official default structure may not be sufficient; the following structure can be adopted:

mysite/
    manage.py
    mysite/
        settings/
            __init__.py
            base.py
            dev.py
            prod.py
        urls.py
        asgi.py
        wsgi.py

    apps/
        blog/
        users/
        orders/

    templates/
    static/
    media/

Advantages:

  • • Split apps by module for clarity and maintainability
  • • Configuration files split by environment
  • • Centralized management of static files and templates

5. A Complete Django Project Workflow

  1. 1. Browser accesses URL
  2. 2. <span>urls.py</span> routing assignment
  3. 3. Corresponding <span>views.py</span> executes business logic
  4. 4. Operate <span>models.py</span> (database layer)
  5. 5. Return template or JSON response
  6. 6. If there is a backend requirement, manage data through <span>admin.py</span>.

6. Conclusion

In this article, you learned:

✔ The default structure and functionality of a Django project

✔ The role of manage.py

✔ The responsibilities of settings / urls / wsgi / asgi

✔ The purpose of each file in a Django app

✔ The best structure for enterprise-level Django projects

With an understanding of these structures, you can truly start writing Django web applications, and subsequent topics such as models, views, templates, and ORM queries will be much easier to grasp.

Leave a Comment