django-planet

Blog: Adam Johnson

web https://adamj.eu/
Author Adam Johnson

Boost Your DX bundle deal

Oct. 15, 2023 » Adam Johnson » [Archived Version]

I released Boost Your Git DX nearly two weeks ago. It’s the second in my “Boost Your DX” series, following last year’s Boost Your Django DX. Both books aim to improve your development experience with their respective tool. Today I’m happy to announce the bundle deal for …

Read More

My fourth appearance on Django Chat

Sept. 20, 2023 » Adam Johnson » [Archived Version]

I’ve once more had the pleasure of joining Carlton and Will on the Django Chat podcast, in Episode #146. We spoke on Tuesday, just hours after Django 5.0 alpha 1 was released, on several topics: Favourite features coming in Django 5.0 My new book, Boost Your Git …

Read More

Django: Move a template tag library into builtins

Sept. 14, 2023 » Adam Johnson » [Archived Version]

Django’s template engine has an underappreciated builtins option that selects libraries to preload in every template. Making a library a builtin avoids the need for an explicit {% load %} tag whenever you use its tags or filters. Putting key libraries in builtins can shorten your templates and make development a …

Read More

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