Django -- How to have a project wide templatetags shared among all my apps in that project
Solution 1
I don't know if this is the right way to do it, but in my Django apps, I always place common template tags in a lib
"app", like so:
proj/
__init__.py
lib/
__init__.py
templatetags/
__init__.py
common_tags.py
Just make sure to add the lib
app to your list of INSTALLED_APPS
in settings.py
.
Solution 2
Since Django 1.9, it is no longer necessary to create additional common
app as others mentioned. All you need to do is to add a path to your project templatetags
directory to the settings.py
's OPTION['libraries']
dict.
After that, these tags will be accessible throughout your whole project. The templatetags
folder can be placed wherever you need and can also have different name.
Customized example from the Django docs:
OPTIONS={
'libraries': {
'myapp_tags': 'path.to.myapp.tags',
'project_tags': 'project.templatetags.common_extras',
'admin.urls': 'django.contrib.admin.templatetags.admin_urls',
},
}
Solution 3
Django registers templatetags globally for each app in INSTALLED_APPS
(and that's why your solution does not work: project is not an application as understood by Django) — they are available in all templates (providing they was properly registered).
I usually have an app that handles miscellaneous functionality (like site's start page) and put templatetags not related to any particular app there, but this is purely cosmetic.
Solution 4
This is the place where you place it in settings.py.
The first key('custom_tags' in this case) in 'libraries' should be what you want to load name in template.
PROJECT STRUCTURE
mysite
- myapp
- mysite
- manage.py
- templatetags
- custom_tags.py
SETTINGS.PY
TEMPLATES = [
{
'BACKEND': '....',
'OPTIONS': {
'context_processors': [
...
],
'libraries': {
'custom_tags': 'mysite.templatetags.custom_tags',
}
},
},
]
un33k
Val Neekman is a Solutions Architect with a passion for Typescript-Angular, Python-Django, UX and Performant Software. He is the Principal Consultant at Neekware Inc.
Updated on June 02, 2022Comments
-
un33k almost 2 years
Second time asking more details ...
I'd like to have a project wide templagetags directory to have the common tags used by all Apps, then each app can have their own tags if need so.
Let say that I have:
proj1/app1 proj1/app1/templatetags/app1_tags.py proj1/app2 proj1/app2/templatetags/app2_tags.py proj1/templatetags/proj1_tags.py proj1/templates/app1/base.html proj1/templates/app1/index.html proj1/templates/app2/base.html proj1/templates/app2/index.html
Where:
proj1/templates/app1/base.html ----------- {% load proj1_tags %} {% load app1_tags %} proj1/templates/app1/index.html ----------- {% extends "base.html" %} proj1/templates/app2/base.html ----------- {% load proj2_tags %} {% load app2_tags %} proj1/templates/app2/index.html ----------- {% extends "base.html" %}
Would this work? It didn't work for me. It can't find the proj1_tags to load.
-
andilabs almost 10 yearshow you than call it in html files? Just
{% load common_tags %}
? -
mipadi almost 10 years@andi: Yes, that's it.
-
Mohammad Torkashvand over 8 yearsThe name of the module inside tempaltetags is the same as the package/application name (e.g. lib.py).
-
Hakim almost 7 yearsJust to make it clearer, in the template html where this tag is called one needs to
{% load project_tags %}
first before calling the tag normally using{% common_extras %}
. -
User almost 6 years
All you need to do is to add a path to your project
can you elaborate on this part and give an example project structure showing the templatetags directory not inside of an app? -
User almost 6 yearsI seem to have got it working for a
templatetags
directory at the same level ofmanage.py
. Using'mytest_tags': 'templatetags.mytest_tags'
. I have__init.py__
inside of templatetags directory and also a python file namedmytest_tags.py
-
Yi Yang Apollo about 5 yearsAdd a public app for all common templatetags and functions. This options works for me. Django version 2.17
-
logicOnAbstractions over 3 yearsDon't know if this is the right way either, but the smartest solution I have come across while trying to manage that. It makes me realize that apps in django can be meta-apps in a way, not just corresponding strictly to a project functionality....
-
MartinM over 2 yearsOPTIONS is under TEMPLATES in Django 2.1.2