'Django can't find templates with jinja2 engine
I'm going to change django's template engines to jinja2, but after setting the setting.py of my project, jinja2 engines doesn't work, the followings are my codes.
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'DIRS': [
BASE_DIR / 'templates',
],
'APP_DIRS':True,
'OPTIONS':{
'environment':'emotion.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',
],
},
},
]
the following is jinja2.py
jinja2.py
def environment(**options):
env = Environment(**options)
env.globals.update(
{
'static':static,
'url':reverse,
}
)
return env
But while I runserver, it seems like jinja2 doesn't work. jinja2 doesn't search the file I want.
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine jinja2:
This engine did not provide a list of tried templates.
Using engine django:
django.template.loaders.app_directories.Loader: D:\Anaconda3\envs\Web\lib\site-packages\django\contrib\admin\templates\catalog\jinj.html (Source does not exist)
django.template.loaders.app_directories.Loader: D:\Anaconda3\envs\Web\lib\site-packages\django\contrib\auth\templates\catalog\jinj.html (Source does not exist)
django.template.loaders.app_directories.Loader: D:\Emotion\emotion\catalog\templates\catalog\jinj.html (Source does not exist)
The django engine is perfect work but I change the file name to test where it search. While I rename it back to correct name, django's engine work, jinja2 doesn't.
My project name is 'emotion', which has a app name catalog. html's path is emotion/catalog/templates/catalog/jinja.html
Solution 1:[1]
Check the setting's path because you could be using the wrong url of DIRS in TEMPLATES of jinja2 in settings.py.
It should be
'DIRS': [
BASE_DIR / 'catalog/templates',
]
instead of
'DIRS': [
BASE_DIR / 'templates',
]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Braiam |
