Can't use Jinja elements on Page - django

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',
],
},
},
]

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',
...

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.

Related

Templates does not exist

How to specify a path to base_site.html that would have views the blog saw it.
enter image description here
You have to do 2 things
Edit the templates settings to include templates folder
Call the template by ‘admin/base_site.html’
First of all add this in your settings.py
'DIRS': [os.path.join(BASE_DIR / "templates")]
For Example:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'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',
],
},
},
]
Now change the path or just paste this, and let me know..
return render(request, "admin/base_site.html", {"tasks":tasks})

TemplateDoesNotExit error when deploying project on heroku

I am trying to deploy my project on heroku but I am stumbling on the above error. Everything works fine when i run the code locally. Below are my setting and views file
template settings
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'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',
],
},
},
]
views of my Home app
def Home(request):
return render(request,'Home/index.html')
Try to use this ( In settings.py ) :-
TEMPLATES = [
{
'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',
'django.template.context_processors.media',
],
},
},
]
Thanks for your suggestion. I solved the error by renaming my templates from 'index.html' to 'main.html'

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?
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.
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
},
]

Context Processors are not working with Jinja2 in Django

Context Processors are not working with Jinja2(Ver 2.10) in Django(Ver 2.0.5). This is what i have done. Created a context processor as follows:
def test_con_proc(request):
return {
'test_con_proc': "Testing Context Processors",
}
And, called it in my template using this:
{{ test_con_proc }}
Also, added this to settings.py file like this:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'DIRS': [....)
],
'APP_DIRS': True,
'OPTIONS': {
'environment': '....jinja2.environment',
},
},
{
'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',
'dashboard.context_processors.test_con_proc',
],
},
},
]
So, What't the proper solution for resolving using context processors with Jinja2 in Django?
You need to install django-jinja then:
change: 'BACKEND': 'django.template.backends.jinja2.Jinja2',
to "BACKEND": "django_jinja.backend.Jinja2",
After that move your context processor to jinja OPTIONS['context_processors']
So your settings should look similar to this:
TEMPLATES = [
{
'BACKEND": "django_jinja.backend.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',
'dashboard.context_processors.test_con_proc',
]
}
},
]

Adding template path Django

I Have been reading about templates in the django book, the author has suggested to add template path in the setting.py variable TEMPLATE_DIRS but my setting.py has no `TEMPLATE_DIRS'(I understand why is it so)and I could find the below
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(os.path.dirname(__file__), ’templates’).replace(’\\’,’/] #1
'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',
],
},
},
]
1)Does the above change I have madein the settings.py lead to me in the correct direction or is there any better way to do it? I wanna make myself sure before I put my self into others and end up in mess.
TEMPLATE_DIRS variable is deprecated since Django 1.8 (https://docs.djangoproject.com/en/1.9/ref/settings/#template-dirs).
Your setup is fine, but you can clean it up a bit, for example:
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
os.path.join(BASE_DIR, 'templates', 'some_other_dir'),
# other paths
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.template.context_processors.media',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]