October 27th 2015

ImportError: cannot import name add_to_builtins under Django 1.9

Whilst upgrading various projects to Django 1.9, I found myself repeatedly searching for the following code snippet so I offer it below as a more permanent note for myself and to aid others.

If you used django.template.base.add_to_builtins to avoid tedious and unsightly {% load module %} blocks in your template files, under Django 1.9 you will get the following traceback:

Traceback (most recent call last):
  File "django/core/management/__init__.py", line 324, in execute
  File "django/__init__.py", line 18, in setup
  File "django/apps/registry.py", line 108, in populate
  File "django/apps/config.py", line 202, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
  File "myproject/myproject/utils/models.py", line 1, in <module>
    from django.template.base import add_to_builtins
ImportError: cannot import name add_to_builtins

The solution is to move to defining settings.TEMPLATES instead of calling add_to_builtins. This replaces a number of your existing settings, including TEMPLATE_CONTEXT_PROCESSORS, TEMPLATE_DIRS, TEMPLATE_LOADERS, etc.

For example:

    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': [
        os.path.join(BASE_DIR, 'templates'),
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
        'builtins': [

Simply add the modules you previously loaded with add_to_builtins to the builtins key under OPTIONS.

(You can read more in the release notes for Django 1.9, as well as read about settings.TEMPLATES generally.)

You can subscribe to new posts via email or RSS.