TemplateDoesNotExist, Django has the good path but can't open the file - python

I have a problem with my Django project : the Templates engines search at the good path but they don't find it... I tried everything :/
I think my settings.py file is ok :
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'public',
]
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',
],
},
},
]
and here my view code :
from django.shortcuts import render
def enregistrement(request):
return render(request, 'public/register.html')
I put my register.html in C:\SitePetitDejs\public\templates\public (yes the path is strange but it symplifies the problem)
And, when I go on http://127.0.0.1:8000/public/register I have "TemplateDoesNotExist at /public/register" error
The strangest thing is that the engines search at the good path :
django.template.loaders.filesystem.Loader: C:\SitePetitDejs\templates\public\register.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\victo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\admin\templates\public\register.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\Users\victo\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\contrib\auth\templates\public\register.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\SitePetitDejs\public\templates\public\register.html (Source does not exist)
Very blur behavior from the engines...
Maybe they don't have the permissions ? (The file is readable...)
I don't know how to solve this problem, I hope will be able to help me :)
Thankss

Related

Instead having a base.html in global templates directory , template does not exist error is coming

I am not able to extend base.html inside templates folder in app 'todo'. base.html is present inside templates folder in main directory.
base.html is present in templates folder but it's showing in the error this file is not existing in source folder.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'todo'
]
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',
],
},
},
]
this is setting.py updated.

django 1.9 and registration/login.html

I'm working on a django 1.9 project.
With Django 1.7.7, login functionnalities was working, but now all the time I have : registration/login.html : Template Does Not Exist
The templates login.html, logout.html are present in 'webgui/template/registration/' and I didn't modified them.
Here some of my settings.py :
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'webgui',
]
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'project.urls'
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',
],
},
},
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'NCIS.db'),
}
}
STATIC_URL = '/static/'
LOGIN_REDIRECT_URL = '/login/'
LOGOUT_URL = '/logout/'
DIRS = (
join(BASE_DIR, 'webgui/template/registration'),
join(BASE_DIR, 'webgui/template/')
)
And my urls.py :
from django.conf.urls import url
from django.contrib import admin
from django.contrib.auth.views import login, logout
import webgui.views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', login, name='login.html'),
url(r'^login/$', login, name='login.html'),
url(r'^logout/$', logout, name='logout.html'),
url(r'^homepage/$', webgui.views.homepage),
url(r'^addproject/$', webgui.views.addproject)
]
What's wrong? I checked the Django docs, but that's the default behaviour.
This question appears first in search engine with searching for
registration/login.html : Template Does Not Exist
So for those coming through search engine, please note that this template doesn't come by default with Django. If you haven't created it, that's why you are getting this error
Here is how you can create a simple one
https://simpleisbetterthancomplex.com/tutorial/2016/06/27/how-to-use-djangos-built-in-login-system.html
Try to put your template dirs paths in DIRS list inside TEMPLATES setting. (Anyway, your template folder name should be templates not template.)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [join(BASE_DIR, 'webgui/template/registration'),join(BASE_DIR, 'webgui/template/')],
'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',
],
},
},
]
After upgrading my django to 1.9.1, same thing happened to me. Apparently, there are updates on templates directory.
Here is how I fixed it.
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',
],
},
},
]
Of course you should have BASE_DIR defined
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
After that,I've deleted the templates folder and created a templates folder for each app. So, inside the each app, just create templates and put the html files inside.
Also in views, connect it to the html file like this.
def index(request):
context_dict = {}
return render(request, 'index.html', context_dict)
This worked for me.
You have set 'APP_DIRS': True,, so Django will search for templates directories inside each app in INSTALLED_APPS, including your webgui app.
The problem is that you have named your directory webgui/template/ instead of webgui/templates/, so the app loader won't find it.
The easiest fix is to rename your directory. If you don't want to do this, you'll have to add the webgui/template directory to your DIRS option.

Django TemplateDoesNotExist Error on Windows machine

I have been following the tutorial for Django Tango with Django
I was trying to add a template as instructed on the link.
I am working with Python 2.7, Django 1.8 on a windows 7 machine.
Below is the error that I get:
TemplateDoesNotExist at /rango/
rango/index.html
Request Method: GET
Request URL: http://127.0.0.1:8000/rango/
Django Version: 1.8
Exception Type: TemplateDoesNotExist
Exception Value:rango/index.html
Exception Location: C:\Python27\lib\site-packages\django\template\loader.py in get_template, line 46
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
C:\Python27\lib\site-packages\django\contrib\admin\templates\rango\index.html (File does not exist)
C:\Python27\lib\site-packages\django\contrib\auth\templates\rango\index.html (File does not exist)
Below is my file structure:
tango_with_django_project
+-- rango
| +--views.py
| +--other files
+-- tango_with_django_project
| +--templates
| | +--rango
| | | +--index.html
| +--settings.py
| +--other files
+-- db.sqlite3
+-- manage.py`
I have given the template path as below in settings.py:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_PATH = os.path.join(BASE_DIR,'templates')
TEMPLATE_DIRS = (
TEMPLATE_PATH,
)
And have set the view as in views.py:
from django.shortcuts import render
from django.http import HttpResponse
def index(request):
context_dict = {'boldmessage': "I am bold font from the context"}
return render(request, 'rango/index.html', context_dict)
Without using the template, it works fine and displays plain text. But when I do the changes required for the template, it does not work and throws the Error.
There are many links on SO with the same issue but none have worked for me.
The one with almost the same Error description and on Windows machine has a vague answer.
I have tried directly specifying the absolute path instead of getting it from os.path and have also tried placing the template folder in different paths.
Any help would be appreciated.
The solution that worked for me was removing the below piece of code from 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',
],
},
},
]
And adding :
TEMPLATE_DIRS = ('C:/Users/vaulstein/tango_with_django_project/templates',)
OR
Changing the line 4th line below :
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['C:/Users/vaulstein/tango_with_django_project/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',
],
},
},
]
You can simply add the application name to INSTALLED_APPS list in settings.py
find the following list in settings.py,
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles'
]
add your application name to that list. In your case,
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'tango_with_django_project'
]
In my case modifying INSTALLED_APPS doesn't help, so based on previos answers I just modified my settings.py in the following way:
Add import OS
Add os.getcwd() into DIRS
So it looks like this:
...
from pathlib import Path
import os
...
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.getcwd()],
'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 1.8 TEMPLATE_DIRS being ignored

This is driving me crazy. I've done something weird and it appears that my TEMPLATE_DIRS entries are being ignored. I have only one settings.py file, located in the project directory, and it contains:
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'),
os.path.join(BASE_DIR, 'web_app/views/'),
)
I'm putting project-level templates in the /templates folder, and then have folders for different view categories in my app folder (e.g. authentication views, account views, etc.).
For example, my main index page view is in web_app/views/main/views_main.py and looks like
from web_app.views.view_classes import AuthenticatedView, AppView
class Index(AppView):
template_name = "main/templates/index.html"
where an AppView is just an extension of TemplateView. Here's my problem: when I try to visit the page, I get a TemplateDoesNotExist exception and the part that's really confusing me is the Template-Loader Postmortem:
Template-loader postmortem
Django tried loading these templates, in this order:
Using loader django.template.loaders.filesystem.Loader:
Using loader django.template.loaders.app_directories.Loader:
C:\Python34\lib\site-packages\django\contrib\admin\templates\main\templates\index.html (File does not exist)
C:\Python34\lib\site-packages\django\contrib\auth\templates\main\templates\index.html (File does not exist)
Why in the world are the 'templates' and 'web_app/views' directories not being searched? I've checked Settings via the debugger and a breakpoint in views_main.py and it looks like they're in there. Has anyone had a similar problem? Thanks.
What version of Django are you using? TEMPLATE_DIRSis deprecated since 1.8
Deprecated since version 1.8:
Set the DIRS option of a DjangoTemplates backend instead.
https://docs.djangoproject.com/en/1.8/ref/settings/#template-dirs
So try this instead:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
# insert your TEMPLATE_DIRS here
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
# list if you haven't customized them:
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Here's a link to an upgrade guide: https://docs.djangoproject.com/en/1.8/ref/templates/upgrading/

Getting TemplateDoesNotExist from Django 1.8

** I'm using Django 1.8. The templates feature has changed in this release of Django. Read more here Upgrading templates to Django 1.8**
This is bothering me because I've come across this issue and fixed it for one of my other projects, but I can't for the life of me figure out how to fix it this time around. I've gone through countless stackoverflow questions and tried to resolve the issue using the answers provided by I've had no luck. This is the error message I am getting:
Exception Type: TemplateDoesNotExist
Exception Value:
index.html
Exception Location: /Library/Python/2.7/site-packages/django/template/loader.py in get_template, line 46
Python Executable: /usr/bin/python
Python Version: 2.7.6
Python Path:
['/Users/User1/Documents/PyCharmProjects/Project1',
It seems that is it looking in the wrong folder, it should be looking under Project1/templates according to my settings.py file:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_PATH = os.path.join(BASE_DIR, '/templates/')
TEMPLATE_DIRS = (
TEMPLATE_PATH,
)
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
#'django.template.loaders.eggs.load_template_source',
)
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'Project1.urls'
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',
],
},
},
]
My templates folder is in the root folder of my project. What's the issue here? I have given it a TEMPLATE_DIRS parameter, and used a proper BASE_DIR, which is what the majority of the answers recommend.
remove the slashes: TEMPLATE_PATH = os.path.join(BASE_DIR, 'templates')
See here
Things have changed with Django 1.8, in which the template system has been improved. See the release notes.
In your settings.py add:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [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',
],
},
},
]
the code above comes straight from one of my projects.
Feel free to use os.path.join(BASE_DIR, 'templates') instead of catenating the strings.
1) If your route for templates is project_name/app_name/templates/app_name
'DIRS': [os.path.join(BASE_DIR, 'app_name', 'templates', 'app_name')],
2) If your route for templates is project _name/templates or project_name/app_name/templates_only
'DIRS': [#leave it just empty, will work fine],
Note: 'template_only' means that templates/*.html there is no any folder inside templates.
I would try placing TEMPLATE_PATH in DIRS:
TEMPLATES =[
{....
'DIRS' : [TEMPLATE_PATH,],
....

Categories