Colpaisa
Colpaisa

Reputation: 365

Use custom directory for "templatetags"

some one know if its likely use custom directory for "templatetags" eje: "my-project/templatetags"

Normal
My-Project-Name
  My-App
    __init__.py
      templatetags
        __init__.py

Need Some Like This

My-Project-Name
  templatetags
    __init__.py

Upvotes: 7

Views: 6125

Answers (2)

rain01
rain01

Reputation: 1284

This is possible. Just add location of your templatetags.py or templatetags directory to Django settings.py into OPTIONS in TEMPLATES setting.

In my case I put my templatetags in the libs/ directory that is located in project root dir.
You have two options, builtins or libraries:

TEMPLATES = [{
    ...
    'OPTIONS': {
        ...
        'builtins': [
            'libs.templatetags'
        ],
        # or as @x-yuri pointed out, you can put them in `libraries`
        'libraries': {
            'my_tags': 'libs.templatetags',
        },
    }
}]

If you use builtins, it is available everywhere and you don't need to use {% load %} for that.
If you use libraries you need to use {% load my_tags %}.

Upvotes: 14

karthikr
karthikr

Reputation: 99660

This is not possible. The reason being, templatetags must reside inside a django app.

From the documentation of templatetags:

Custom template tags and filters must live inside a Django app. If they relate to an existing app it makes sense to bundle them there; otherwise, you should create a new app to hold them.

The app should contain a templatetags directory, at the same level as models.py, views.py, etc. If this doesn’t already exist, create it - don’t forget the __init__.py file to ensure the directory is treated as a Python package. After adding this module, you will need to restart your server before you can use the tags or filters in templates.

Upvotes: 2

Related Questions