Django: What is the best practice of structuring global templatetags?

I understand that templatetags are mainly used for applications that are INSTALLED_APPS, for example articles / templatetags /, but in my case I need tags for general things like navigation that don't have an application.

I am currently saving templatetags in my project. and in order to be picked up, I added my project to INSTALLED_APPS - it works, but I'm not sure if it’s right to do it - are there any flaws?

+4
source share
1 answer

I would do it the same way Django provides its additional template tags, that is, it creates its own package / application ( django.contrib.humanize , django.contrib.markup , django.contrib.webdesign )

These are just three “normal” packages, inside of which there are templatetags . The name of the module inside tempaltetags is the name of the package / application (for example, humanize.py ).

Then put it somewhere where Python can find it.

You can also create some kind of “meta-package” templatetags and put everything there, for example.

 templatetags - navigation - __init__.py - templatetags - _init__.py - navigation.py - other - ... 

Of course, you should add them to you INSTALLED_APPS (for example, templatetags.navigation ) and load them into your template (for example, {% load navigation %} ).

+6
source

Source: https://habr.com/ru/post/1301281/


All Articles