Download the Django template tag library for all default views

I have a small templatetag typographic library that I use on almost every page. Right now I need to download it for each template using

{% load nbsp %} 

Is there a way to download it “globally” for all kinds and templates at once? Inserting a download shortcut into the base template does not work.

+44
django django-templates
Jul 26 '09 at 16:43
source share
4 answers

There is an add_to_builtins method in add_to_builtins . Just pass it the name of your templatetags module (as a string).

 from django.template.loader import add_to_builtins add_to_builtins('myapp.templatetags.mytagslib') 

Now mytagslib is available automatically in any template.

+69
Jul 26 '09 at 17:17
source share

In django 1.7 just replace for from django.template.base import add_to_builtins

+25
Jul 23 '14 at 16:01
source share

It will change with the release of Django 1.9.

Starting with version 1.9, the correct approach will be to configure template tags and filters under the builtins OPTIONS key - see the example below:

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

More details: https://docs.djangoproject.com/en/dev/releases/1.9/#django-template-base-add-to-builtins-is-removed

+24
Aug 11 '15 at 18:13
source share

Django 1.9 has a libraries tag labels and dotted paths in Python template tag modules for registration in the template engine. This can be used to add new libraries or provide alternative labels for existing ones.

 TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], 'libraries': { # Adding this section should work around the issue. 'custom_tags' : 'myapp.templatetags.custom_tags',#to add new tags module, 'i18n' : 'myapp.templatetags.custom_i18n', #to replace exsiting tags modile }, }, }, ] 
+3
May 2 '16 at 7:13
source share



All Articles