Django Framework : How To Create A Well Django Project Structure

Mar 30 2023 . 5 min read

In this article we are going to see how to structure our django project nicely.

Here we are going to see all the steps we have to keep in our mind while creating any django project, and these steps are really helpful to create a better django project.

1. Always Create a Virtual Environment

Before creating a django project always create a virtual environment in your system, inside a directory.

To create a virtual environment that helps you to install all your dependencies or packages in virtual environment instead of installing globally.


Open your terminal and follow these steps to create virtual environment.


  • Install virtualenv package
pip install virtualenv

  • Create virtual environment by using this command virtualenv <env_name>
virtalenv env

  • Activate virtua environment by using this command
souce env/bin/activate


All set, now we can install any dependencies, that is going to be isolated.

2. Add the requirements.txt File

requirements.txt file is a special file that contains all the dependencies or modules or packages to run the application.


To create a requirements.txt file, just you need to use this command.

pip freeze > requirements.txt


Make sure the virtual environment should be active, so that it will only include the dependencies from the current environment.

3. Adding .env File

By adding .env file to our project that helps to hide our secrets information like api key, database credentials etc.


Follow these steps to setup .env file to django project :


  1. Create a .env file to the root project where manage.py file is located and add the sceret credentials.
  2. .env
    1. SECRET_KEY=django-insecure-&qdb)shq2fa4!$^j9226d8f#_a2l#a#r0)-$(j2q0&1emgb4mg
    2. # DATABASE CREDENTIALS
    3. DATABASE_NAME=TEST_DB
    4. DATABASE_USER=TEST_USER
    5. DATABASE_PASSWORD=TEST_PASSWORD
    6. DATABASE_HOST=localhost
    7. DATABASE_PORT=54321
    8. # AWS CREDENTIALS
    9. AWS_ACCESS_KEY_ID=TEST_KEY_AKIQ4745YVBTSYDD_ID
    10. AWS_SECRET_ACCESS_KEY=TEST_SECRET_uskaidewtll6nIbkperakgssewuiw_KEY
    11. AWS_STORAGE_BUCKET_NAME=TEST_BUCKET
    12. AWS_S3_REGION_NAME=eu-west-1

  3. Install python-dotenv package. by using this command
  4. pip install python-dotenv

  5. Open settings.py file and load dotenv at the top of settings.py file.
  6. settings.py
    1. from dotenv import load_dotenv
    2. load_dotenv()

  7. Use env variables in settings.py file
  8. settings.py
    1. from dotenv import load_dotenv
    2. load_dotenv()
    3. SECRET_KEY = os.environ.get("SECRET_KEY")
    4. # AWS SETTINGS
    5. AWS_ACCESS_KEY_ID = os.environ.get("AWS_ACCESS_KEY_ID")
    6. AWS_SECRET_ACCESS_KEY = os.environ.get("AWS_SECRET_ACCESS_KEY")
    7. AWS_STORAGE_BUCKET_NAME = os.environ.get("AWS_STORAGE_BUCKET_NAME")
    8. AWS_S3_REGION_NAME = os.environ.get("AWS_S3_REGION_NAME")


All set, this how to use .env file to secure the secret credentials in django application.

4. Must Include .gitignore File

Try to must add .gitignore file in our project. .gitignore file prevent to push irrelvent files to the server.


Create a .gitignore file in our root project where manage.py file is located, and here we can add the files which we don't want to push to the server.

.gitignore
  1. .env
  2. __pycache__
  3. .DS_Store
  4. .idea
  5. db.sqlite3


We can add list of files, which we don't want to push to server, we can include these files in .gitignore file.

5. Formatting & Linting

Our code should be formatted, it look our code more readable and more beautiful.


To format our code, we can install these two dependencies : black, flake8

pip install black flake8


Let's apply black & flake8 to format & linting our code :

or e.g., if you want to format the views.py file code, just use this command.

black app/views.py
flake8 app/views.py

6. Templates & Static

Templates and Static are two most important components of our project, make sure there should be only one templates and static folder in our project, hence it's easy to maintain.


Create templates and static folder in our root project, where manage.py file is located.

7. Dockerize The Project

Docker helps to run the application, without installing or setup the project in the machine.


Follow these steps to dockerize the django project :


1. Create a docker-compose.yml in our root project, where manage.py file is located and paste this code.

Dockerfile
  1. FROM python:3.10
  2. ENV PYTHONUNBUFFERED=1
  3. WORKDIR /code
  4. COPY requirements.txt .
  5. RUN pip install -r requirements.txt
  6. COPY . .
  7. CMD ["python","manage.py","runserver","0.0.0.0:8000"]


2. Create a docker-compose.yml in our root project, where manage.py file is located and paste this code.

docker-compose.yml
  1. version: '3.10'
  2. services:
  3.     django:
  4.        image:django-docker:0.0.1
  5.        build:.
  6.        ports:
  7.            -"8000:8000"


3. Now we can use docker-compose to create docker images of our project.

docker-compose up --build

8. Modularization Settings File

Settings.py file is a main file of our project and its contains everyting related to our project settings, and to maintain these settings for both production and development is kind of complex part, so to sort out this problem, we can divide our settings file into two part one is for production and second is for development part, hence its easy to maintain.

Conclusion

In this artcle, we have discussed few important things that we have to keep in our mind, while creating any django project. These are really helpful to create and structure a better django project.

I hope you find this article helpful, and if you have anything that you could share with us, comments are open for everyone.