Django Framework : How To Create A Well Django Project Structure
Table of Content
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
- Create virtual environment by using this command virtualenv <env_name>
- Activate virtua environment by using this command
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.
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 :
- Create a .env file to the root project where manage.py file is located and add the sceret credentials.
- SECRET_KEY=django-insecure-&qdb)shq2fa4!$^j9226d8f#_a2l#a#r0)-$(j2q0&1emgb4mg
- # DATABASE CREDENTIALS
- DATABASE_NAME=TEST_DB
- DATABASE_USER=TEST_USER
- DATABASE_PASSWORD=TEST_PASSWORD
- DATABASE_HOST=localhost
- DATABASE_PORT=54321
- # AWS CREDENTIALS
- AWS_ACCESS_KEY_ID=TEST_KEY_AKIQ4745YVBTSYDD_ID
- AWS_SECRET_ACCESS_KEY=TEST_SECRET_uskaidewtll6nIbkperakgssewuiw_KEY
- AWS_STORAGE_BUCKET_NAME=TEST_BUCKET
- AWS_S3_REGION_NAME=eu-west-1
- Install python-dotenv package. by using this command
- Open settings.py file and load dotenv at the top of settings.py file.
- from dotenv import load_dotenv
- load_dotenv()
- Use env variables in settings.py file
- from dotenv import load_dotenv
- load_dotenv()
- SECRET_KEY = os.environ.get("SECRET_KEY")
- # AWS SETTINGS
- AWS_ACCESS_KEY_ID = os.environ.get("AWS_ACCESS_KEY_ID")
- AWS_SECRET_ACCESS_KEY = os.environ.get("AWS_SECRET_ACCESS_KEY")
- AWS_STORAGE_BUCKET_NAME = os.environ.get("AWS_STORAGE_BUCKET_NAME")
- 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.
- .env
- __pycache__
- .DS_Store
- .idea
- 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
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.
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.
- FROM python:3.10
- ENV PYTHONUNBUFFERED=1
- WORKDIR /code
- COPY requirements.txt .
- RUN pip install -r requirements.txt
- COPY . .
- 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.
- version: '3.10'
- services:
- django:
- image:django-docker:0.0.1
- build:.
- ports:
- -"8000:8000"
3. Now we can use docker-compose to create docker images of our project.
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.