Using Jinja2 but keeping the template directory as templates.

Hi everyone :wave:

I want to use Jinja2 as the templating language for Django, but as per the docs, Django would look inside the jinja2 subfolder in each registered app for templates. I don’t want that but want it to be the default template directory itself (which is the case for the Django’s default templating system). Any ideas ?

You could create a subclass of Jinja2, set the app_dirname attribute on that class to “templates”, and use that class as your template engine.

Is there an example in the docs about how I might go about this approach ?

How, in Python, do you define a class that is a child of (inherits from) a parent class?

For a basic idea of what needs to be done, take a look at the source code for the Jinja2 class in django.template.backends.jinja2.

Thanks for the help. Also based on the docs, Templates | Django documentation | Django, in order to use Django specific tags in a Jinja2 environment, the docs tells about setting an environment option (which points to an environment function).

Could you tell what this option is ? Is it a setting we can set in settings.py ?

Sorry, I’ve never used Jinja2 - if the information available in the Jinja2 class docs doesn’t give you enough to start with, I’m not sure I can be of any help here.

Hopefully someone else reading this thread can jump in with some information.

1 Like

Thanks a lot. The environment option is defined in the OPTIONS list for the template backend (It just was buried among other details). I am now able to configure Jinja2 properly.