Skip to content

Tips

This section gathers tips, copy and paste configurations and package recommendations that I use quite often in my projects to solve specific problems.

Settings

If there is a setting in settings.py or elsewhere that you don't understand, go to the official django settings reference page and press Ctrl + F to search for it. I used the django-production package to configure the production settings which I then customized. I have removed the package as a dependency, but I advise you to go and check for yourself what is available.

Dynamic web pages

HTMX for simple interactive elements, django-unicorn if I need something more integrated with django. It's not a binary choice, you can use both, the main advantage for me is the simplicity compared to a frontend javascript framework.

Note

If you use htmx boost + debug toolbar (already included in the template), you will need this.

Task queues and schedulers

Task queues are used to offload tasks to a dedicated worker process when the processing of those tasks does not fit into a traditional request-response cycle. Basically, if you need to do something that might take too long to process and whose result does not need to be shown immediately to the user, you use a queue manager. Schedulers are used to periodically run tasks. There are many options available in the django third-party ecosystem, some focus solely on providing a task queue, others are just schedulers and many of them provide both in one package. You can also search for purely python solutions and integrate them into your django project yourself.

I prefer options that do not require additional infrastructure (redis, rabbitmq, etc.) for simple tasks. For more complex tasks, I tend to choose a solution that supports redis as a task broker.

Doesn't require setup of external tools, redis, rabbitmq, etc..

Require the setup of external tools, redis, rabbitmq, etc.

Note

The order matters, that's the order in which I would choose one of these packages.

If you are using one of these you might want an automatic reload feature when files changes, you can use the hupper python package for that purpose. If your project was generated with fuzzy-couscous then it is already declared as a dev dependencies.

Media storage

Media files in django usually refer to files uploaded by users, profile pictures, product images, etc. I usually manage my media files using django-storages. Here is how I set it up.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# core/storages.py
from storages.backends.s3boto3 import S3Boto3Storage

class MediaRootS3Boto3Storage(S3Boto3Storage):
    location = "media"
    file_overwrite = False


# settings.py - production settings
AWS_ACCESS_KEY_ID = env("DJANGO_AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY = env("DJANGO_AWS_SECRET_ACCESS_KEY")
AWS_STORAGE_BUCKET_NAME = env("DJANGO_AWS_STORAGE_BUCKET_NAME")
DEFAULT_FILE_STORAGE = "project_name.core.storages.MediaRootS3Boto3Storage"
MEDIA_URL = f"https://{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com/media/"

Database backup

Whenever possible, take advantage of a fully managed database solution, they usually offer automatic backup of your databases. In my opinion, this is the best option if you don't want to deal with the hassle of managing your own database.

For specific postgresql options, see their hosting support page.

However, if for some reason you want / need to manage your database yourself and just want an automatic backup solution then django-dbbackup is what you need. You can use one of the scheduling packages discussed above to periodically run the backup command.

Health check your django project

Health check is about making sure that your django application and related services are always available / running. My go-to package for this is django-health-check. After installing and configuring django-health-check, you need to associate it with an uptime monitoring service, this is the service that will periodically call your health-check endpoint to make sure everything is fine. Here is a list of available options.

Read more on the health check pattern here.

Extra that I haven't tried myself yet