Before going to deep dive in django, we must know the django project directory structure. It is important to have a clear understanding of project directory structure. Python Django web framework follow MVT structure. MVT stands for model, view & template structure. In this article we will understand the Django project directory structure in detail.
Django project directory structure
As you already know the command to create new django project. django-admin startproject myproject
The above command will create a folder with name as myproject shown in below;
The main directory of django is the name you specify when creating the project. This directory serves as the container for the entire application and is where you’ll find some important files & folder that will be used for the application development & deployment cycle.
1. manage.py
Manage.py is very important file to run commands in your projects. You should never change anything in the file. Always keep as it is. It is responsible for many admin tasks that you can perform directly via running the commands. When we create a new project, python django framework will automatically create the file. You can perform many tasks as mentioned below using manage.py
- Running the Development Server: You can start the development server using python manage.py runserver. This is very important for development.
- Creating Database Tables: The
python manage.py makemigrations
andpython manage.py migrate
commands are used to create and apply database schema changes. Django’s ORM (Object-Relational Mapping) handles this process, making it much more manageable than writing SQL queries manually. - Creating a New Application: You can create a new module (Django application) within your project using
python manage.py startapp appname
. - Managing Users: manage.py provides commands for managing users, such as creating superusers, resetting passwords, and more.
- Running Tests: Django has a built-in testing framework, and you can run tests using python manage.py test.
- Generating Documentation: You can use manage.py to generate documentation for your project’s models and views.
2. The Project Name Directory
Within this folder you will find few important files named as __init__.py, settings.py, urls.py, **wsgi.py and asgi.py. Now lets us understand little more about these files.
__init__.py : This file is basically python packaging file. This file will be called when you import modules in the application. If you remove this file from your application import will fail as when you import the package this file gets called automatically. You can define package level variables also in the file but it is less recommended.
For eg. suppose you have a one folder called my_package, inside my_package you have 2 files my_package/greetings.py
my_package/__init__.py
So when you want to use greetings.py somewhere else in the application, due to __init__.py file you can simply import like belowfrom my_package import greetings
and you can use your methods defined in greetings.py files.
settings.py: This is a crucial configuration file where you specify settings for your Django project. It includes database configurations, middleware settings, installed applications, internationalization settings, and much more. Customizing settings.py is often necessary to tailor Django to your specific project’s needs. In settings.py you need to add middleware’s, installed apps, some directory configs, static files like images or css path directory config. Database also can be configured in settings.py file.
urls.py : As name suggests urls.py is the main center of the django project where we define our routings. urls.py will map every route to views.py to call specific method for every route you define. This is essential for defining the navigation structure of your web application.
wsgi.py / asgi.py: These files are entry points for serving your Django application using WSGI (Web Server Gateway Interface) or ASGI (Asynchronous Server Gateway Interface) servers, depending on your deployment requirements. WSGI is suitable for traditional web servers like Apache and Nginx, while ASGI is used for asynchronous and real-time applications.
3. Template Directory
Template directory will not be automatically created when you create new django project. You need to create. Template directory basically consists of your HTML page that will be rendered when an user visits your django project. Django templates support inheritance, which means you can create a base template that contains the common structure of your site (e.g., header, footer, navigation), and then extend this base template in other templates.
This was all about the Django project directory structure that all beginners should know.