Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 212
Colpaisa Avatar asked Jul 07 '14 02:07

Colpaisa


People also ask

How do I register a custom template tag?

Create a custom template tagUnder the application directory, create the templatetags package (it should contain the __init__.py file). For example, Django/DjangoApp/templatetags. In the templatetags package, create a . py file, for example my_custom_tags, and add some code to it to declare a custom tag.

What is custom tags in Django?

Django offers a variety of built-in template tags such as {% if %} or {% block %}. However, Django also allows you to create your own template tags to perform custom actions. The power of custom template tags is that you can process any data and add it to any template regardless of the view executed.


1 Answers

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 %}.

like image 194
rain01 Avatar answered Oct 07 '22 01:10

rain01