Best Practices for a Django Working Directory Structure

Vaibhav Vaibhav Jun 03, 2021
Best Practices for a Django Working Directory Structure

Django is a fantastic Python-based, open-source web development framework that you can use to create full-stack web applications. Since it’s a framework, one can initially expect many files inside the projects. It can be too overwhelming for a beginner at first, but there is nothing to worry about; it will all look simple with time.

A web application has two sides: development and production. The development side has different settings in comparison with the production side. Django applications have many folders such as the static, media, templates, etc. Since Django has been around for a while, you need to know the best practices for a Django working directory structure for more successful projects in this area. We’ll talk about these methods here in the article, so read on!

Django Directory Structure

The directory structure of a Django project should look something like this:

Project Folder/
    .gitignore    
    LICENSE.rst or LICENSE.md
    Documentation/
    README.rst or README.md
    environment
    requirements.txt
    MyProject/
        manage.py
        media/
        MyProject/
            __init__.py
            settings/
                __init__.py
                base.py
                development.py
                production.py
                .env
            urls.py
            wsgi.py
        AppOne/
            static/
                AppOne/
            templates/
                AppOne/
            urls.py
            views.py
            models.py
        AppTwo/
            static/
                AppTwo/
            templates/
                AppTwo/
            urls.py
            views.py
            models.py
        AppThree/
            static/
                AppThree/
            templates/
                AppThree/
            urls.py
            views.py
            models.py

Django Root Folder

The root folder, Project Folder, contains Django projects and all the other files related to the project, such as the license, README, environment, requirements.txt, .gitignore, and Documentation or Docs.

Django Project Folder

Inside the Django project, MyProject, there should be the manage.py file, the media folder, the MyProject folder that contains the settings.py file, and the Django applications.

Django Project Settings Folder

By default, Django provides us with a single settings.py file and some other important files such as the urls.py, wsgi.py, etc. The settings.py file contains settings for both production and development. Hence, it can grow in length for a more significant project.

Therefore, splitting the settings into their respective files is a great idea. The base.py file contains the settings that apply to both development and production. The development.py file contains all the applicable settings for development, and the production.py has all the production settings.

The development and production sides can have different database settings, password validation settings, email settings, payment settings, API keys, and other configurations.

If you’re using Python Decouple to store sensitive data, the .env file should also be in this folder.

Django Applications

Every Django Application will have a static folder and a templates folder. Inside these folders, there will be another folder by the name of the application. Inside these folders, we will place all static files and templates.

This approach allows us to have multiple files with the same name. For example, each application might have an index.html file and a style.css, and it will become easier to access them.

----------
HTML Files
----------
{% url 'AppOne/index.html' %}
{% url 'AppTwo/index.html' %}
{% url 'AppThree/index.html' %}

---------
CSS Files
---------
{% static 'AppOne/style.css' %}
{% static 'AppTwo/style.css' %}
{% static 'AppThree/style.css' %}
Vaibhav Vaibhav avatar Vaibhav Vaibhav avatar

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.