Override existing Django Template Tags

I was looking for the same answer, so figured I’d share my solution here. I wanted to override the default url template tag in django without having to use a custom template tag and load it in every template file.

The goal was to replace %20 (spaces) with + (pluses). Here’s what I came up with…

In __init__.py

from django.template.defaulttags import URLNode

old_render = URLNode.render
def new_render(cls, context):
  """ Override existing url method to use pluses instead of spaces
  """
  return old_render(cls, context).replace("%20", "+")
URLNode.render = new_render

This page was useful https://github.com/django/django/blob/master/django/template/defaulttags.py

Leave a Comment