Templates does not exist - django

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})

Related

Django TemplateDoesNotExist. Django doesn't check one of the my apps, when looking for templates

Django can't find needed template from my app. I named directory "app/template/app/template.html", my 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',
],
},
},
]
Also I add my app name in INSTALED_APPS.
I tried move my template in project's directory and its work! But I just wanna understand, why loader didn't check my app directory

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

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.