55

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

{% load nbsp %}

Is there a way to load it "globally" for all views and templates at once? Putting the load tag into a base template doesn't work.

Tomas Andrle
  • 13,132
  • 15
  • 75
  • 92

4 Answers4

76

There is an add_to_builtins method in django.template.loader. 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.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • 25
    Note that while you can do this, it's quite likely that you'll regret it at some point (I've done it, and regretted it). It makes your templates non-portable to any other project which doesn't add_to_builtins, and it can break tests which render those templates (unless you make sure the test runner also runs add_to_builtins). All in all, it makes things more brittle for a very small gain in convenience. – Carl Meyer Jul 27 '09 at 13:06
  • 20
    Not to mention that any new developer is going to be confuzzled by your use of a tag that doesn't exist in the standard libraries, until they ask you (if you're still around) or stumble upon it. :) Remember, explicit is better than implicit. – Xiong Chiamiov Jul 27 '09 at 20:16
  • Can we use this to override default firstof and cycle tag that are XSS faulty? https://code.djangoproject.com/ticket/17906 – Natim Jul 16 '13 at 08:50
  • Actually I am using `{% filter force_escape %}{% firstof var1 var2 var3 "fallback value" %}{% endfilter %}` solution now. – Natim Sep 25 '13 at 09:20
  • Where do I put this code (ideally) if I want to make the tamplate tag available in my entire project? – JacobF Feb 19 '14 at 10:53
  • 1
    It's better not to use `add_to_buildins` anymore as it is **totally undocumented and can be moved/removed** in any moment without any earlier deprecation. Soon to be released django 1.7 is good examply of it (see [django repo](https://github.com/django/django/commit/34263c67b4315bb326c15d23bdf0363b3a372e17)) - **this solution will not work with django 1.7**. – glowka Apr 16 '14 at 12:19
  • 1
    With the new import path given below, this approach still appears to be working: from django.template.base import add_to_builtins – Simon Steinberger Sep 02 '14 at 23:13
  • You may have to also add a blank __init__.py file in appname/templatetags/ directory – Tejasvi Hegde Nov 25 '14 at 14:36
39

It will change with Django 1.9 release.

Since 1.9, correct approach will be configuring template tags and filters under builtins key of OPTIONS - see the example below:

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

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

pbajsarowicz
  • 542
  • 6
  • 12
28

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

AMIC MING
  • 6,306
  • 6
  • 46
  • 62
bsao
  • 411
  • 4
  • 7
  • 3
    Why the downvote? This saved my day, because the accepted answer is outdated! The import path has changed in Django 1.7. Thanks bsao! – Simon Steinberger Sep 02 '14 at 23:12
  • 2
    Should this be a comment or an edit on the [existing answer](http://stackoverflow.com/a/1185049/1075247) ? It does not make sense on it's own. – AncientSwordRage May 12 '15 at 10:15
6

In Django 1.9 there is an libraries dictionary of labels and dotted Python paths of template tag modules to register with the template engine. This can be used to add new libraries or provide alternate 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
            },
        },
    },
]
kartheek
  • 6,434
  • 3
  • 42
  • 41