Django TemplateDoesNotExist Error on Windows machine - python

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

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.

How to use template from project in another app?

I'm thinking in organizing all my templates in inside my project: scolarte.
As suggested by this question:
What is the best location to put templates in django project?
If you can’t think of an obvious place to put your templates, we
recommend creating a templates directory within your Django project
(i.e., within the mysite directory you created in Chapter 2, if you’ve
been following along with our examples).
But I need to call it from another app. The view is called but getting error:
TemplateDoesNotExist at /cuentas/ingreso/
scolarte/templates/scolarte/registration/signup.html
I even tried to put the full path to the template in project folder:
roles/views.py:
class SignUpView(TemplateView):
template_name = 'scolarte/templates/scolarte/registration/signup.html'
# don't work neither
#template_name = 'templates/scolarte/registration/signup.html'
#template_name = 'scolarte/registration/signup.html'
#template_name = 'registration/signup.html'
roles/urls.py:
from django.urls import include, path
from .views import SignUpView, SellerSignUpView, ClientSignUpView
urlpatterns = [
path('ingreso/', SignUpView.as_view(), name='signup'),
]
scolarte/urls.py
urlpatterns = [
path('', include('core.urls')),
path('cuentas/', include('roles.urls')),
path('admin/', admin.site.urls),
]
My app is orgnized like this:
roles
|_migrations
|_templates
...
|_urls.py
|_views.py
scolarte #project name
|_templates
|_scolarte
|_registration
|_signup.html
|_setting.py
|_urls.py
setting.py
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',
],
},
},
]
UPDATE 1:
roles app:
** roles app - view.py **:
UPDATE 2:
Your path to the template is incorrect by standard Django convention, but let me show you first how to fix it. What you'll want to do is make sure in settings.py you have these settings made. This is from a Django 3.0 fresh project creation.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__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',
],
},
},
]
If you use those, and follow the suggested directory structure of:
scolarte
|_scolarte
|_settings.py
|_urls.py
(etc..)
|_templates
|_scolarte
|_registration
|_signup.html
Then you can use with this path:
template_name = 'scolarte/registration/signup.html'

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

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

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.

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