Dr. Roger Ianjamasimanana

Complete django project structures

By Dr. Roger Ianjamasimanana

In a standard django project, there are multiple folders and files that organize everything from configuration to application logic and assets (templates and static files). Below is a typical django project structure layout to help you better structure your website and to avoid confusion.


1. Overall structure of a django project

A complete django project typically has the following structure:
myproject/
|-- manage.py
|-- myproject/
|   |-- __init__.py
|   |-- settings.py
|   |-- urls.py
|   |-- wsgi.py
|   |-- asgi.py
|
|-- templates/
|   |-- base.html
|   |-- myapp/
|       |-- index.html
|
|-- static/
|   |-- css/
|   |   |-- style.css
|   |-- js/
|       |-- app.js
|
|-- myapp/
    |-- __init__.py
    |-- admin.py
    |-- apps.py
    |-- migrations/
    |   |-- __init__.py
    |-- models.py
    |-- tests.py
    |-- views.py

2. Project-level files and folders

2.1 The manage.py file

The manage.py file is the command-line utility that ties your django project’s settings to various administrative commands. We use manage.py to:

  • Run the development server:
    python manage.py runserver
  • Create and apply database migrations:
    python manage.py makemigrations
    python manage.py migrate
  • Generate new apps:
    python manage.py startapp myapp
  • Create admin users:
    python manage.py createsuperuser
  • Perform other project-level management tasks

2.2 The django project directory

The myproject/ directory shares its name with your entire project. It holds important configuration files:

  • __init__.py : this file is necessary to indicate that the directory is a python package.
  • settings.py : This is the central configuration for the project (e.g., INSTALLED_APPS, MIDDLEWARE, DATABASES, TEMPLATES, STATIC_URL).
  • urls.py : this is the main URL configuration where you define your urls.
  • wsgi.py : WSGI entry point for production servers (like Gunicorn or uWSGI).
  • asgi.py : ASGI entry point, enabling async features (like channels, WebSockets).

2.3 The django templates directory

The templates/ folder stores your HTML files. You might place a base.html file here for a common layout and subdirectories matching each app name. In settings.py, make sure you have APP_DIRS = True in TEMPLATES, or configure DIRS to point to this directory if you prefer a project-level approach.

Example structure:

templates/
|-- base.html
|-- myapp/
    |-- index.html
I advise to always separate the html files of each app in your django project in separate directory as follows:

templates/
|-- base.html
|-- myfirstapp/
    |-- index.html
|-- mysecondapp/
    |-- index.html
|-- mythirdapp/
    |-- index.html
The myfirstapp, mysecondapp, etc. should be the exact name of your application defined in:
python manage.py startapp myfirstapp

2.4 The static/ directory

This directory contains your static files (CSS, JavaScript, images). Each app can also have its own static/ folder, but placing them here at the project level can help organize shared assets:


static/
|-- css/
|   |-- style.css
|-- js/
    |-- app.js

In settings.py, you typically set STATIC_URL (like /static/) and STATICFILES_DIRS to help django locate these files in development.


3. The App-level directory structure

The app-level directory (myapp/) is an individual “app” within your django project, designed to hold models, views, URLs, and logic for a specific feature or set of features. Multiple apps can exist in a single project.

The __init__.py file

This marks the myapp/ folder as a python package. This file is typically empty, but it is necessary for python’s package system.

3.1 The admin.py file

This registers your app’s models to appear in the django’s built-in admin interface. For example:


from django.contrib import admin
from .models import Product

admin.site.register(Product)
By doing this, you will see the Product model in your django admin.

3.2 The apps.py file

This file contains an AppConfig subclass. Django uses it to set up the app. This is generated automatically when you run python manage.py startapp myapp.

3.3 The migrations/ folder

This houses the database migration files that reflect changes to your models. The __init__.py inside this folder makes it a package. Each migration file corresponds to a particular alteration in your data schema.

3.4 The models.py file

This defines classes mapping to the database tables. Each class inherits from django.db.models.Model. For instance:

from django.db import models

class Product(models.Model):
    name = models.CharField(max_length=255)
    price = models.DecimalField(decimal_places=2, max_digits=6)

3.5 The tests.py file

This file contains tests that ensure your app’s features work as expected. Use python manage.py test to discover and run these tests.

3.6 The views.py file

This file handles the logic for incoming HTTP requests and returns responses such as HTML (rendered from your templates) or JSON data. For example:

from django.shortcuts import render
from .models import Product

def product_list(request):
    products = Product.objects.all()
    return render(request, 'myapp/index.html', {'products': products})

By following the project structure outlined above, you ensure that your settings, templates, static files, and app code remain logically separated and maintainable. As your project grows, you may create additional apps, each with its own models.py, views.py, etc., while still sharing global configurations like settings.py and unified urls.py.

feature-top
Readers’ comment
feature-top
Log in to add a comment
🔐 Access
 
Related posts