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.
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:
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:
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.
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:
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.
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.
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.
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.
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:
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:
Image 2. Django Shell in Jupyter Notebook
December 18, 2019
Continuosly building, testing, releasing and monitoring t-matix mobile apps