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.
First, create a Django project using the following command:
django-admin startproject myproject
Django automatically creates the 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
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.
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
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.
The __init__.py file is created as part of the project package.
It allows Python to treat the directory as a package.
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.
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.
The DATABASES setting defines the database configuration
used by the Django project.
A new Django project includes a default database configuration.
DATABASES = {
'default': {
...
}
}
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.
The TEMPLATES setting contains configuration for Django's
template system.
Templates are used to create the HTML displayed to users.
The urls.py file contains URL configuration for the project.
It connects requested URLs with views.
For example:
urlpatterns = [
...
]
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.
The asgi.py file provides an entry point for ASGI-compatible
servers.
ASGI supports modern Python web application server interfaces, including asynchronous application capabilities.
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.
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.
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.
Django creates a SECRET_KEY setting in the project settings.
It is used by Django for cryptographic signing and security-related operations.
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.
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.
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.
| 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 |
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.
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.
A clear project structure makes it easier to understand and maintain a Django application.
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.
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.
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.
The most important files in the basic Django project are:
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.
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.Question: Which file contains the main configuration settings of a Django project?