django-planet

Adam Johnson

Blog Info Adam Johnson
Blog website Link

Django: The perils of string_if_invalid in templates

Aug. 8, 2023 » Adam Johnson » [Archived Version]

Django’s template engine has a string_if_invalid option that replaces missing variable lookups with a string of your choice: TEMPLATES = [ { "BACKEND": "django.template.backends.django.DjangoTemplates", # ... "OPTIONS": { # ... "string_if_invalid": "MISSING VARIABLE %s", }, } ] The %s will be replaced with the name of the missing variable. This exists as a debugging aid to …

Read More

Django: Clean up unused code with Vulture

July 11, 2023 » Adam Johnson » [Archived Version]

As projects evolve, old functionality gets removed. Often such deletions are incomplete, leaving in their wake unused functions, classes, and other code objects. Unused code is clutter that brings no joy: it hinders comprehension, taxes codebase-wide quality improvements, and can sometimes lead to errors if later used. In an ideal …

Read More

Django: Flush out test flakiness by randomly ordering QuerySets

July 3, 2023 » Adam Johnson » [Archived Version]

Sometimes code depends on the order of a QuerySet whilst not specifying an order. This can lead to random, flaky test failures because databases can return rows in any order when none is specified. The problem is made worse by some databases, notably PostgreSQL, which nearly always return rows in …

Read More

Django: A version of json_script for pre-serialized JSON strings

July 1, 2023 » Adam Johnson » [Archived Version]

Django’s json_script template filter is a convenient and safe way to pass a larger amount of data to JavaScript. I covered it in my post last year How to Safely Pass Data to JavaScript in a Django Template. I found an interesting use case for json_script in my client …

Read More

Django: A security improvement coming to format_html()

June 14, 2023 » Adam Johnson » [Archived Version]

Can you spot the problem with this Django snippet? from django.utils.html import format_html def user_snippet(user): return format_html(f"<em>{user.name}</em>") Well, the problem is that format_html() is passed an already-templated string! Its escaping ability is not being used. If the user name contains HTML, it …

Read More

Django: Avoid database queries in template context processors

March 23, 2023 » Adam Johnson » [Archived Version]

Django’s template engine allows you to augment template contexts with context processors. These are functions that take the current request and return a dictionary to be merged into the context: from example.models import HotDog from example.models import HotDogState def hot_dog_stats(request): return { "hot_dogs_eaten": HotDog.objects.filter( state …

Read More

Django: Parameterized tests for all model admin classes

March 17, 2023 » Adam Johnson » [Archived Version]

Here’s an application of “test smarter, not harder”, as per Luke Plant’s post. I came up with this recently whilst working on my client Silvr’s project, and I’m pretty proud of it. It should apply to any project using Django’s admin. When you declare a …

Read More

Django: How to profile and improve startup time

March 2, 2023 » Adam Johnson » [Archived Version]

Your Django project’s startup time impacts how smooth it is to work with. Django has to restart your project every time you run a management command and when runserver reloads. This involves importing all your apps, and thus all the modules that they import. As you add more code …

Read More

How to migrate from Django’s PostgreSQL CI Fields to use a case-insensitive collation

Feb. 23, 2023 » Adam Johnson » [Archived Version]

If you upgrade to Django 4.2, you may see system check warnings like: example.User.email: (fields.W906) django.contrib.postgres.fields.CIEmailField is deprecated. Support for it (except in historical migrations) will be removed in Django 5.1. HINT: Use EmailField(db_collation="…") with a case-insensitive non-deterministic collation instead …

Read More

django-upgrade release with Django 4.2 fixers

Feb. 17, 2023 » Adam Johnson » [Archived Version]

I just released django-upgrade 1.13.0. The headline features are some new fixers targetting Django 4.2, which is currently in alpha. Let’s walk through these new fixers. For more detail on any of them, see the Django 4.2 README section. New headers argument in tests Recent-ish …

Read More