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