Django: add i18n template tag to built-ins

Django 1.9 allows you to load template tags globally, i.e. without having to include {% load %} statements at the top of each template. The new option is called builtins and you can set it in your settings.py like so (in the example, I’m loading the i18n tag library).

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'builtins': ['django.templatetags.i18n'],
            'context_processors': [
                ...
            ],
        },
    },
]

Its a good idea (in terms of keeping the code reusable/portable) to use builtins for tag libraries required by every or almost every template in your project. Keep using {% load tag %} for less common tags.

This entry was posted in Programming and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *