Blog Info | Adam Johnson |
---|---|
Blog website | Link |
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 MoreJuly 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 MoreJuly 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 MoreJuly 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 MoreJune 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 MoreMarch 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 MoreMarch 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 MoreMarch 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 MoreFeb. 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 MoreFeb. 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