Al Mahdi
Al Mahdi

Reputation: 810

how to use jinja2 in Django 3.1

Now I am using the Django 3.1 template engine but I am not satisfied with it.

But I see that jinja2 template engine is very powerful that it.

Thought Django says it has support for jinja2 template engine and I was following this Django documentation, but I couldn't use that.

# settings.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.jinja2.Jinja2',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    }
]

Browser Error:

("Encountered unknown tag 'url'. Jinja was looking for the following tags: 'endfor' or 'else'. The innermost block that needs to be closed is 'for'.",)

So, please tell me how do I do it?

Upvotes: 4

Views: 3756

Answers (2)

apet
apet

Reputation: 1098

here are docs: https://niwinz.github.io/django-jinja/latest/

install jinja2

pip install django-jinja

add into INSTALLED_APPS

INSTALLED_APPS = (
         .......
    'django_jinja',
         .......
)

add into template engines list:

TEMPLATES = [
    {
        "BACKEND": "django_jinja.backend.Jinja2",
        "APP_DIRS": True,
        "OPTIONS": {
           "match_extension": ".jinja",
        }
     },
     {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [],
        "APP_DIRS": True
     },
]

Upvotes: 2

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476624

You can use multiple engines, but then the directories should be non-overelapping, or you use the engines with a given priority, if you specify with the DIRS setting [Django-doc] what directories belong to which template. But here both are the same, so that means Django will always select the first one.

You thus specify:

# settings.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.jinja2.Jinja2',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    }
]

We thus do not add any items to the DIRS setting for the DjangoTemplates.

Upvotes: 1

Related Questions