Django Framework — Best Practices

django logo

Django is an open source python based framework that helps to build web applications. The popularity of this framework increased rapidly in last couple of years. Compare to other python-based frameworks like Flask and Pyramid, django framework is far most popular among python developers. It supports both Python 2.7 version and Python 3.6 version.

Google trends of the 3 most popular Python web development frameworks

The Django design philosophies are :

  • Lose Coupling

As a python and django developer, I am writing this article to explain the good practices for Django framework. I assume that the readers are using a Linux Ubuntu Machine. Lets Begin:

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 updated often. When we developing multiple python application in the same local machine, it’s really hard to keep track of each and every packages. It’s not possible to use different version of the packages for multiple projects. Sometimes updating a package for one project can break other project setups.

The solution for the above problem is, using Virtual Environments. To install virtual environment please follow following instructions.

  1. Install pip first
    sudo apt-get install python3-pip

Holding your project dependencies (packages) in a virtual environment on your machine allows you to keep them in an isolated environment. You only use them for a single (or multiple) projects. When creating a new virtual environment, you’re starting a fresh environment with no packages installed in it.

Lets say you want to install django 1.11 in your virtual environment for your current project. First activate the specific virtual environment and type
(project_name) $ pip install Django==1.11

for installing version 1.11 of Django accessible only from within the environment. Neither your main Python interpreter nor the other virtual environments on your machine will be able to access the new Django package you’ve just installed.

2. Maintaining requirements.txt file

Requirements are the list of Python packages (dependencies) of your project that are used by the project while it runs. It includes the version for each package. Here’s an example for a requirements.txt file:


Maintaining your requirement.txt up-to-date is very important for collaborating with other developers. It’s also helps to keep your production environment properly configured. When we include requirement.txt file in your code repository, it enables you to update all the installed packages in your virtual environment by executing a single line in the terminal. Then you can get new developers up and running in no time.

If a new developer joins the team, or if you want to configure a new environment using the same packages listed in the requirements.txt file, execute in the virtual environment context:
(project_name) $ pip3 install -r requirments.txt

All requirements listed in the file will immediately be installed in your virtual environment. Older versions will be updated and newer versions will be downgraded to fit the exact list of requirements.txt.

I highly recommend integrating these commands to your work flow. Update the requirements.txt file before pushing code to the repository and install requirements.txt file after pulling code from the repository.

3. Better configuration

The settings module is the only true requirement for a Django project. Typically, it places in the root of your project as contains the main and most essential configurations for the project. It is very straightforward. But sometimes, as a developer working on a team, or when setting up a production environment, you need more than one basic file.

There is an extended approach for configuring the file. It allows you to maintain different versions and use the one you want at any given time and in any environment.

First, navigate to your file path:

(project_name) $ cd /path/to/settings-file

Then create a new module called settings (module is a folder containing an file):

(project_name) $ mkdir settings

Now, rename your file to and place it inside the new module you created:

(project_name) $ mv settings/

For this example, I assume that you want to configure one settings file for your development environment and one for your production environment. Different developers on the same team can use the exact same approach for defining different settings files.

For your development environment create:

(project_name) $ nano settings/

Then type:

from .base import *

DEBUG = True

For your production environment create:

(project_name) $ nano settings/

and type:

from .base import *

DEBUG = False

Now, whenever you want to add or update the settings of a specific environment, you can easily do it in its own settings file.

You might be wondering — how does Django know which settings file to load on each environment? That’s what the file is used for. When Django looks for the it used to load when running the server, for example, it now finds a settings module rather than a file. But as long as it’s a module containing an file, as far as Django is concerned, it’s the exact same thing. Django will load the file and execute whatever is written in it.

Therefore, we need to define which settings file we want to load inside the file by executing:

(project_name) $ settings/

In this blog we’ve covered three best practices for better setting up a Django project:

  • Working inside a virtual environment

Software Engineer @ hSenid Mobile | Former Software Engineering Intern @ WSO2 | Software Engineering Undergraduate @ IIT

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store