Question

New to Django. In the Django docs, I know it says "Custom template tags and filters must live inside a Django app.". I am going to have 4 or 5 apps, each with custom template tags (some of which will be the same tags as those in other apps). Even though it says that, is it possible to make one master folder for all of my template tags at the root of my project? It seems like having separate folders for each app violates D.R.Y....

Was it helpful?

Solution

Well, there's no reason to have duplicate tags; a template tag library in any app can be loaded from any template. You should only have one copy of any tag.

That said, what's commonly done is to create a template tag APP to house all template tags.

Just build a blank application called tags, utils, whatever, and put all template tags in that app.

root/utils/templatetags

It doesn't even need a model.py/urls.py to function in installed_apps.

OTHER TIPS

You could make an app thats only purpose is to handle some general things like template tags or utils. Sometimes I do this and just call it "app".

But even if you keep things the way they are, there is no reason to duplicate template tags because they are available in all templates anyway. Unless you plan on breaking the apps up and using them in different projects, just put the template tag in the appropriate app, and load it from a template.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top