A Beginner guide to Django project directory structure

Django project directory structure

  • 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 and python 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.

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 below
from 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.

Leave a Reply

Your email address will not be published. Required fields are marked *