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.
40👍
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
- [Django]-Is it better to use path() or url() in urls.py for django 2.0?
- [Django]-How to get the domain name of my site within a Django template?
- [Django]-What are the options for overriding Django's cascading delete behaviour?
- [Django]-Django ORM – objects.filter() vs. objects.all().filter() – which one is preferred?
- [Django]-Default value for user ForeignKey with Django admin
- [Django]-How to change User representation in Django Admin when used as Foreign Key?
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
},
},
},
]
- [Django]-What is the advantage of Class-Based views?
- [Django]-How about having a SingletonModel in Django?
- [Django]-Pass request context to serializer from Viewset in Django Rest Framework
Source:stackexchange.com