Lesson 10 of 60 – Django Project Structure
17%

📁 Django Project Structure

In this lesson, you will learn about the files and folders that Django creates when you create a new project. Understanding the project structure is important because each file has a specific purpose.

Note: A Django project contains the main configuration of your website, while Django apps are used to organize individual features or parts of the application.

1. Create a Django Project

First, create a Django project using the following command:

django-admin startproject myproject

Django automatically creates the basic project structure.

2. Basic Project Structure

A newly created Django project generally looks like this:

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

3. Outer Project Folder

The outer myproject folder is the directory that contains the Django project's files and inner project package.

You can give the outer directory another name when organizing your project.

4. manage.py

manage.py is one of the most important files in a Django project.

It provides a command-line interface for performing many Django project management tasks.

python manage.py runserver

5. Inner Project Package

The inner myproject directory is the Python package that contains the main project configuration files.

It normally contains settings.py, urls.py, asgi.py and wsgi.py.

6. __init__.py

The __init__.py file is created as part of the project package.

It allows Python to treat the directory as a package.

7. settings.py

The settings.py file contains the main configuration settings for your Django project.

It controls important areas such as installed applications, middleware, templates, databases and static files.

8. INSTALLED_APPS

Django uses the INSTALLED_APPS setting to keep track of applications that are enabled in the project.

INSTALLED_APPS = [
    ...
]

Django's built-in applications are included here by default.

9. DATABASES

The DATABASES setting defines the database configuration used by the Django project.

A new Django project includes a default database configuration.

DATABASES = {
    'default': {
        ...
    }
}

10. MIDDLEWARE

The MIDDLEWARE setting contains middleware components that process requests and responses as they pass through the Django application.

Django includes several middleware components in a new project.

11. TEMPLATES

The TEMPLATES setting contains configuration for Django's template system.

Templates are used to create the HTML displayed to users.

12. urls.py

The urls.py file contains URL configuration for the project.

It connects requested URLs with views.

For example:

urlpatterns = [
    ...
]

13. URL Patterns

URL patterns tell Django which view should handle a particular URL.

The project's main urls.py file can also include URL patterns from individual Django apps.

14. asgi.py

The asgi.py file provides an entry point for ASGI-compatible servers.

ASGI supports modern Python web application server interfaces, including asynchronous application capabilities.

15. wsgi.py

The wsgi.py file provides an entry point for WSGI-compatible servers.

It is commonly used when deploying Django applications with WSGI-based server configurations.

16. Project Settings and Configuration

Django keeps many project-wide configuration options inside settings.py.

For example, settings can control the database, installed apps, middleware, templates, language and static files.

17. BASE_DIR

A default Django project contains a BASE_DIR variable in settings.py.

It represents the base directory of the project and is commonly used when constructing paths for project files.

18. SECRET_KEY

Django creates a SECRET_KEY setting in the project settings.

It is used by Django for cryptographic signing and security-related operations.

Important: Keep the secret key protected and do not expose it unnecessarily.

19. DEBUG

The DEBUG setting controls Django's debug mode.

DEBUG = True

Debug mode provides detailed error information during development. It should be configured appropriately for production environments.

20. ALLOWED_HOSTS

The ALLOWED_HOSTS setting defines which host/domain names can serve the Django application when host validation is enabled.

ALLOWED_HOSTS = []

This setting is commonly updated when configuring a project for deployment.

21. Project Structure and Apps

A Django project can contain multiple apps.

For example, a student management project might contain apps for students, courses and payments.

Each app can have its own models, views, URLs, templates and other files.

22. Project vs App

Project App
Contains overall configuration Provides a specific feature or functionality
Can contain multiple apps Can be part of a project
Contains settings and main URL configuration Usually contains models, views and templates

23. Example Project with an App

After creating an app, a project might look like:

myproject/
    manage.py
    myproject/
        __init__.py
        settings.py
        urls.py
        asgi.py
        wsgi.py
    students/
        ...

Here, myproject contains the project configuration and students is a Django app.

24. Running Commands from the Project Directory

Most Django management commands are run from the directory containing manage.py.

python manage.py runserver

Other management commands will be introduced throughout the course.

25. Project Structure Helps Organization

A clear project structure makes it easier to understand and maintain a Django application.

  • Project configuration stays organized.
  • Apps can handle separate features.
  • URLs can be organized logically.
  • Templates and static files can be managed separately.

26. Common Project Structure Mistake

Beginners sometimes confuse the outer project directory with the inner Python project package.

Remember that the inner project package contains files such as settings.py and urls.py, while manage.py is normally located in the outer project directory.

27. Complete Example Structure

MyDjangoProject/
│
├── manage.py
│
└── myproject/
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    ├── asgi.py
    └── wsgi.py

This is the basic structure generated by Django's startproject command.

28. Run the Project

Once the project has been created, start the development server:

python manage.py runserver

Then open the local address provided by Django in your browser.

29. What You Should Remember

The most important files in the basic Django project are:

  • manage.py – project management commands
  • settings.py – project configuration
  • urls.py – URL configuration
  • asgi.py – ASGI entry point
  • wsgi.py – WSGI entry point
  • __init__.py – Python package file

30. Django Project Structure Summary

A Django project starts with a simple but organized structure. The project package contains the main configuration files, while manage.py provides the command-line interface for managing the project.

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

Understanding these files will make it easier to work with Django as your projects become larger.

📌 Key Points

  • manage.py is used for Django management commands.
  • settings.py contains project configuration.
  • urls.py contains the project's URL configuration.
  • asgi.py provides an ASGI entry point.
  • wsgi.py provides a WSGI entry point.
  • __init__.py is part of the Python project package.
  • A Django project can contain multiple Django apps.
  • The project and app are different concepts.

🧠 Quick Quiz

Question: Which file contains the main configuration settings of a Django project?