Django -- How to have a project wide templatetags shared among all my apps in that project Django -- How to have a project wide templatetags shared among all my apps in that project django django

Django -- How to have a project wide templatetags shared among all my apps in that project


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.


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


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.