kala-tamin Load a Django template tag library for all views by default


Load a Django template tag library for all views by default



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.




Teleport - django custom tag

1:



Adding data to many-to-many field of a modelform within a view
There is an add_to_builtins method in django.template.loader.


Newbie Django Question : Can't find data I thought I preset in a Form
Just pass it the name of your templatetags module (as a string)..
How to test for use of a django template block?
from django.template.loader import add_to_builtins  add_to_builtins('myapp.templatetags.mytagslib') 
Now mytagslib is available automatically in any template..
how to nginx virtual servers + fcgi for django?


Django: Querying read-only view with no primary key


memcached caches request?

2:



django / file uploads permissions
In django 1.7 just replace for from django.template.base import add_to_builtins.


3:


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.


4:


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 }, }, }, ]



71 out of 100 based on 71 user ratings 821 reviews