Django throws error message "'loaders' option is empty" - django

I'm trying to move to the new Django 1.8 settings with regard to templates, and am getting following error message:
Django couldn't find any templates because your 'loaders' option is empty!
Here is my TEMPLATES setting:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'OPTIONS': {
'context_processors': [
'django.contrib.auth.context_processors.auth',
],
},
},
]

"Loaders" also needs to be specified in TEMPLATES if not specified as TEMPLATE_LOADERS:
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
The alternative is to specify 'APP_DIRS': True,.
See https://docs.djangoproject.com/en/1.8/ref/templates/upgrading/

Related

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

How to customize template in django-oscar?

i want to customize django-oscar template according to my requirement , when i modified the checkout page, i had been encoutred with this error :
TemplateSyntaxError at /checkout/thank-you/
Invalid block tag on line 132: 'oscar_thumbnail', expected 'endwith'. Did you forget to register or load this tag?
this my settings.py :
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates'),
OSCAR_MAIN_TEMPLATE_DIR
]
,
# 'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.i18n',
'django.contrib.messages.context_processors.messages',
'oscar.apps.search.context_processors.search_form',
'oscar.apps.promotions.context_processors.promotions',
'oscar.apps.checkout.context_processors.checkout',
'oscar.apps.customer.notifications.context_processors.notifications',
'oscar.core.context_processors.metadata',
],
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
},
},
]
how i can register the templatetag into my custom template in django-oscar?

Django 2.1.7 - "TemplateDoesNotExist at /munichlivingapp/seekers/

Django 2.1.7 - "TemplateDoesNotExist at /munichlivingapp/seekers/"
(The identical issue: Templates Django (Does not exist at/) )
1) The browser error message:
2) My project file structure:
3) The relevant part of my settings.py file:
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',
"django.core.context_processors.media",
],
},
},
]
I tried the 3 solutions I found on Stack Overflow, but none of them worked.
(In the TEMPLATE_DIRS dictionary)
1) Solution #1:
'DIRS': [ os.path.join(BASE_DIR, "templates")],
2) Solution #2:
'DIRS': ['templates'],
3) Solution #3:
TEMPLATE_DIRS = (
' /home/mycode/mysite/templates/',
)
Change template_name='seekers_list.html' to template_name='munichlivingapp/seekers_list.html'
Because you keep your template inside templates/munichlivingapp directory.

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

getting TemplateDoesNotExist error in django 1.9

I am facing strange very issue today. I am getting TemplateDoesNotExist (see first image) but when I tried to debug the template source with debug-toolbar it is correctly showing the templates path (see image 2) More strangely, when I clicked on specific templates button it is correctly showing the source of template.
This is the first time I am facing such issues. Can someone please explain why I am getting this error.
EDIT: Adding settings.py file(relavant portion)
SETTINGS_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(SETTINGS_PATH, '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',
],
},
},
]
Thanks
)
I had the similar issue with Django 1.9 . I just changed the DIRS in TEMPLATES of settings.py file.
Try this
'DIRS': [os.path.join(BASE_DIR,'templates')],
Instead of your
'DIRS':[os.path.join(SETTINGS_PATH,'templates')],
Try this code instead of your TEMPLATES
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',
],
},
},]
I upgraded to 1.9 today and suddenly had the same problem. For me it seems that adding " 'APP_DIRS': True, " to the templates does the trick (I toggled a few times by adding/deleting this and it works/fails).
So what does APP_DIRS do: if I understand the documentation ( https://docs.djangoproject.com/en/1.9/ref/templates/api/ ) correctly it reads the default Django templates if True. Basically, for 95% of all projects this should be the case.