Story by
Ivan Pandžić

Django is a popular open-source web framework written in Python. There are different ways of using it and different tools that can be used while working in it. In this article, I am going to talk about the 10 tools and practices used at t-matix, which are guaranteed to improve your productivity while developing using Django.

0. Development using Docker

Today, everyone and their grandmother knows about Docker, but some developers still have doubts whether the benefits outweigh the disadvantages of using it for development. In my opinion, they do, at least for most projects. Using Docker allows us to match as closely as possible the production environment during the development process. It also enables lightning-fast setup on different machines, i.e. those belonging to other developers and testing/staging/production machines. Since you have to define all the dependencies to run a project in containers, there is almost no doubt the project will also work on other machines. We can effectively forget about the often spoken ‘Works on my machine’ sentence.

1. Make

One drawback of development using Docker (and Docker Compose) are the long commands. This is where Make comes in. Make, a build automation tool created in 1976, is making a comeback big time. In order to use it, we must first create a Makefile, a set of directives which are used to generate a target/goal. You can think of a Makefile as a collection of short shell scripts in a single document. Here is an example of a very basic Makefile:

Copy to Clipboard

As I already mentioned, by using a Makefile together with Docker Compose, we can set up a project from scratch in a matter of minutes and using only two commands:

Copy to Clipboard

Running “make start” builds Docker images, installs the required dependencies, runs the migrations and starts up containers described in the development docker-compose.yml file. The alternative is setting up a virtual environment, installing the dependencies manually and hoping for the best.

2. pipenv

While working with Django, developers usually use a requirements.txt file and pip to install the dependencies (pip install -r requirements.txt) on the development, staging and production machines. Although we can specify the versions of the packages required for the project in requirements.txt (and their variations like base.txt, dev.txt, prod.txt, etc.), these packages also have dependencies that will be installed by pip, and if their versions are not defined, pip will install their latest versions. This can lead to dependency hell, a common problem when using Python and Django in different environments. This usually happens because pip does not have a dependency resolver, but there is a tool that has one, combining functionality of pip and virtual environments: pipenv.

Image 1. “Python Environment”: source: www.xkcd.com

Install simply by using:

Copy to Clipboard

and run:

Copy to Clipboard

3. .env files

One thing that can really make the life of developers easier are the .env files. It goes without saying that we don’t want to hardcode database information and the SECRET_KEY in the project code, so we use the .env files on development, staging, testing and production machines to store this information. This way, we don’t have to create developer-specific Django settings files, which makes setup much faster and more straightforward.

4. Black

After setting everything up, you can start coding. And while you could spend countless hours discussing which style of coding is the most Pythonic, you could also just use Black, a Python code formatter. When using Black, you cede control of Python code formatting to it, which takes your mind off formatting and allows you to focus on finding solutions to problems. Apart from ridding you of unnecessary formatting discussions, uniform code will make things like migrating to new versions of Django and Python much easier.

5. tox

Using Python 3.5 and Django 1.11 and want to switch to Python 3.6 and Django 2.0 or higher? To make this transition an incremental instead of a monumental task, use the tox library. Tox, a powerful testing tool, allows us to test a project against many different environments.

6. changelog

In order to keep better track of all the changes you are making to your code, you can use changelog. For each merge request, you create a new Markdown file in the changelog.d directory with a filename adhering to the #mr.(feature|bugfix|doc|removal|misc).md schema – for example, changelog.d/77.bugfix.md for changes submitted in merge request 77. You can use the changelog directly or through tox. You can also automate it using towncrier and GitLab.

7. GitLab CI/CD

All of the tools mentioned so far can be integrated nicely into GitLab CI/CD. Gitlab is, of course, primarily used for version control, but it also offers other functionalities, such as storing Docker images in its Gitlab Container Registry and running CI/CD pipelines using Gitlab runners, all wrapped in one neat package.

8. PyCharm Professional Edition

Speaking of neat packages with many capabilities, we need to mention PyCharm Professional Edition. Choosing a text editor or an IDE has always been highly subjective for most programming languages, but when it comes to Python, and in particular to Django development, PyCharm is the clear winner. Some of the things that make it my favorite IDE for Python and Django are its refactoring and debugging capabilities, the database tools and the fancy version control integration. If you want a free alternative, the best I can do is Visual Studio Code.

9. IPython

Last and least, I highly recommend IPython for Django development. If you install it as one of your project dependencies, Django will use it instead of the default shell when running:

Copy to Clipboard

Some of the advantages of the IPython shell over the default shell include tab completion, history, and the fact that Linux commands such as “pwd” and “ls” still work in the shell, and you can run python scripts using “run script_name.py” in it, which is not the case with the default shell. After you install the jupyter and django-extensions packages, and add “django_extensions” to INSTALLED_APPS, you can also access the shell using Jupyter Notebook by running:

Copy to Clipboard
Image 2. Django Shell in Jupyter Notebook

Other blogs

PLATFORM
December 18, 2019
Continuosly building, testing, releasing and monitoring t-matix mobile apps

Story by
Josip Virovac

PLATFORM
November 5, 2019
Kubernetes-what is it? And how do we use it?


Story by
GORAN PIZENT

PLATFORM
September 25, 2019
Locust- a Hidden Gem in Load Testing


Story by
Igor Crnković

View all