Django is an open-source python-based framework that helps to build web applications. The popularity of this framework increased rapidly in the last couple of years. Django is one of the easiest frameworks to use and easy to understand.
Django design philosophies are :
- Loose Coupling
- Less Code
- DRY
- Consistency
1.Create Virtual Environment
While developing any python based applications, third-party libraries installation is an inseparable requirement. Keeping them in an organizing manner is an essential thing as those packages are often updated. When we are developing multiple python applications in the same local machine, it’s tough to keep track of each package. It’s not possible to use a different version of the packages for multiple projects. Sometimes updating a package for one project can break other project setups.
How to handle this is by basically using a virtual environment. To install a virtual environment follow the instructions below:
- Install pip
sudo apt-get install python3-pip
- Then install virtualenv using pip3 and create an environment
sudo pip3 install virtualenv env
- Active your virtual environment
source env/bin/activate
- This is the prefix that you will notice on your terminal then you cd to the actual project directory
(env) ➜ Food2Fork git:(master)
To deactivate virtualenv
deactivate
Holding your project dependencies in a virtual environment on your machine allows you to keep them in an isolated environment. You only use them for single or multiple projects. But when creating a new virtual environment, you need to understand that no packages are installed in it.
Requirements.txt file
This is a list of dependencies that enable your project to run effectively. example of a requirements file
bleach==3.2.1
certifi==2020.11.8
chardet==3.0.4
Django==2.2.15
django-markdownify==0.8.1
djangorestframework==3.12.2
humanize==3.1.0
idna==2.10
webencodings==0.5.1
wget==3.2
when working with multiple people it is easier for them to run your project because of the requirements file and to run this file you just do this
(env) ➜ Food2Fork git:(master) ✗ pip3 install -r requirements.txt
and it will install all dependencies needed for that particular project.
3.Setting configuration
The setting file is like the heart of Django inside a project because it contains the configurations of the project. when working with other developers you need to have a settings file for development and production separately. Navigate to your settings.py file path:
(env) ➜ Food2Fork git:(master) ✗ cd /path/to/settings
Then create a new module called settings
(env) ➜ Food2Fork git:(master) ✗ mkdir settings
Now, rename your settings.py file to base.py and place it inside the new module you created:
(env) ➜ Food2Fork git:(master) ✗ mv settings.py settings/base.py
For your development environment create:
(env) ➜ Food2Fork git:(master) ✗ nano settings/development.py
Then type:
from .base import *
DEBUG = True
For your production environment create:
(env) ➜ Food2Fork git:(master) ✗ nano settings/production.py
and type:
from .base import *
DEBUG = False
ALLOWED_HOSTS = [‘app.Food2Fork.com’, ]
Do this whenever you want to add or update settings of a specific environment
Therefore, we need to define which settings file we want to load inside the init.py file by executing:
(env) ➜ Food2Fork git:(master) ✗ settings/__init__.py
we’ve covered three best practices for better setting up a Django project:
- Working inside a virtual environment
- Having a requirements.txt file and updated
- Setting up a better project settings file