Tricking Django's title Filter
2025-10-08Django’s title filter is often used to prettify strings in templates, for example, when rendering
model verbose names in an admin or API interface. When handling abbreviations it can be a bit too eager in capitalising,
though.
What happens if you apply the filter to the following API key model?
class SupplierAPIKey(AbstractAPIKey):
...
class Meta:
verbose_name = "supplier API key"
verbose_name_plural = "supplier API keys"
It capitalises each word in the string, resulting in Supplier Api Key. Ugh… That hurts my brain.
Unicode to the rescue! The zero-width space (\u200b) acts like a regular space for text processing — but, well, has
no width during rendering. By inserting it within the acronym, we can trick the title filter into keeping our
uppercase characters as they are treated as individual words.
"supplier A\u200bP\u200bI key"
Be aware that these characters are still rendered and present. If you copy the following text and insert into a proper
editor, which shows non-printing characters, they show up: Supplier API Key
May your strings be capitalised to your taste.
© 2025 Dennis Stritzke
Code samples are public domain unless otherwise noted.