Can't use Jinja elements on Page

I am working on Django and new to Jinja templates. I am able to print variable from the context but can't use other features of Jinja.

When I do {{ 1+1 }} on the page . it shows:

Could not parse the remainder: '+1' from '1+1'

I am trying to generate a random no. by {{ range(1, 51) | random }} ,As by this answer. but it throws error as:

Could not parse some characters: range|(1, 51)| | random

In settings.py :

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.jinja2.Jinja2',
    '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',
        ],
    },
},
]

2 answers

  • answered 2017-06-17 17:58 Raja Simon

    The problem is you didn't activate Jinja2 template in your Django project. Please set the Template Engine properly.

    TEMPLATES = [
        {
             'BACKEND': 'django.template.backends.jinja2.Jinja2',
             ...
    

  • answered 2017-06-17 17:58 Linch

    TEMPLATES = [{
        'BACKEND': 'django.template.backends.jinja2.Jinja2',
        'BACKEND': 'django.template.backends.django.DjangoTemplates', # remove this line
        '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',
            ],
        },
    }]
    

    In line 3 you override key BACKEND with Django Template Engine.