Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. In this tutorial, I will show how to properly configure a new Django project and best practices to be followed while development. This tutorial also expects that you are a new Django programmer who is looking for initial best practices to be followed while setting up a Django project.
At the time of writing this post Python3.6 and following packages were used in an Ubuntu Linux system.
Package
Version
Django
2.2.14
psycopg2
2.8.5
python-decouple
3.3
virtualenv
20.0.28
We will be creating a virtual environment for our Django project. It is a good practice to create a separate virtual environment for each Django project. You may want to have a different version of Django for different projects in your system. A virtual environment isolates one Django project dependencies from others. Let’s find out how.
We will be using Virtualenv a tool to create isolated Python environments.
Now let’s create an environment where all our python packages will be install.
The above command creates a virtual environment name ‘venv’. You can give any name to your environment. The -p takes the python version you want to use for that environment.
Activate the environment and install Django.
Once all the packages are installed we can now create a new Django project
Lets test the set up by running the project.
By defaut Django uses sqlite as a backend database. But we will be modifying the default configuration by changing some values in settings.py and use PostgreSQL as our backend database server. For installing PostgreSQL you can follow this link.
We will be installing two packages, one for creating a Postgres connection and another one for creating environmental variables for our project.
Now we will be modifying the settings.py file of our project so that it uses PostgreSQL as our database server. Find the DATABASES variable in our settings.py file and replace the value with the following lines of code.
You can see that the Database username, password, and other values are hardcoded in the settings file. It is not a good practice to hardcode any user or system-related credentials in your code. These values are usually read from system environment variables.
We will now modify the DATABASES variable in settings file and instead of hardcoding our database credentials in code, we will ask Django to read it from environment variables. We will be using the python-decouple module to create and read environmental variables for us.
And create a .env file in the root folder of the project (where your manage.py file resides.) and add the following values.
As you can see I have also added the SECRET_KEY, DEBUG, ALLOWED_HOSTS variables to .env file.
Our final settings file will look something like this below.
Test the set up by running the project
After we have set up and ran our project successfully. We have to add our project dependencies to requirements.txt file. Create a requirements.txt file and add all the packages to it. This file is usually created in the root path of the project.
Our code is now ready to ship. Initialize your code repository with Git and add your .env file to .gitignore file. This will ignore your .env file while pushing your code to GitHub or GitLab. You can also create a .env.example file with dummy values for reference.
There are many other advance practices followed by Django developers like creating multiple requirements files, multiple settings files, and multiple environment files for development and production environment separately. But all these set up has more advanced configuration and we can have another tutorial for that. I hope this helped you create and configure Django using environment variables. You can find the code at Github.