Django TemplateDoesNotExist problem while DIR is correctly set - python

I was running this Django project perfectly previously using Python3.7.2 and Django 2.1.5, recently I installed Anaconda3 and changed the interpreter to the Python3.7.1 in it. After reinstalling Django, when I ran the project the TemplateDoesNotExist exception showed up. My OS is Windows 10.
My DIRS is set as below:
TDIR = os.path.join(BASE_DIR, 'Templates/').replace('\\', '/')
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [TDIR],
'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 note is on the error page:
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: C:\ProgramData\Anaconda3\lib\site-packages\django\contrib\admin\templates\welcomePage.html (Source does not exist)
django.template.loaders.app_directories.Loader: C:\ProgramData\Anaconda3\lib\site-packages\django\contrib\auth\templates\welcomePage.html (Source does not exist)
It seems that Django search the Anaconda path for templates instead of the customized path, but I can see on the error page that it was correctly set. This is what the error page shows:
TEMPLATES
[{'APP_DIRS': True,
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['D:/IgnorazWork/SUDoc/SUDoc/Templates/'],
'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'D:/IgnorazWork/SUDoc/SUDoc/Templates/'is exactly where I put the templates. How does this happen?
Thanks!

Try this :
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
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',
],
},
},
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
Note - Make sure that your folder name are exactly same Template\ and template\ are different

Related

Django: Project name is duplicated in the template path

My project structure is roughly as follows:
dinnerproject/
dinnerproject/
settings.py
dinners/
templates/dinners/
main.html
templates/
base.html
manage.py
In settings.py I've got TEMPLATE configured like so:
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 dinners app is added to INSTALLED_APPS.
I use a TemplateView with template_name = "main.html", which extends base.html.
But when I try to open the page that's supposed to return main.html, I keep getting a TemplateDoesNotExist error saying:
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: C:\Users\User\dinnerproject\dinnerproject\templates\main.html (Source does not exist)
(...)
django.template.loaders.app_directories.Loader: C:\Users\User\dinnerproject\dinnerproject\dinners\templates\main.html (Source does not exist)
For some reason, my project name is duplicated in the paths, so django cannot find the right directories. What am I doing wrong?
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'dinnerproject','templates'),
os.path.join(BASE_DIR,'dinners','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',
],
},
},
]
urls.py:
app_name="your app name"
This will solve your error

what should i write in the settings.py to import my Template

I have deployed my project on pythonanywhere and there i am getting an error that template does not exist
It's working properly on local host
File directory
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',
],
},
},
]
Can someone tell what should i add my template DIRS
Try to put this into DIRS
'DIRS': [os.path.join(BASE_DIR, 'Templates')],
after that you can get your html files like 'Main/test.html'
And yeah it's better to put names in lowercase

Why Heroku can't find my Django templates?

I deployed my project on Heroku. But it can't find my templates(Source doesn't exist):
My project architecture:
settings.py:
from pathlib import Path
import sys
import os
BASE_DIR = Path(__file__).resolve().parent.parent
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: All templates from the Account app load with this extending
The problem is solved. There was extra whitespace, like {% extends 'base/base.html ' %}

TemplateDoesNotExist Error in Pythonanywhere

Erorr
I wanna know where django search for index.html
Setting.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['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.py
def index(request):
return render(request, 'index.html', {})
Django will look in the templates directory, that's if you created a folder called templates and if you have a file named index.html in the templates folder. For some reason I feel like the reason why you are asking this question is because you can not get your template to render when you run the server, am I correct? Please get back to me and I can do my best to fill you in so you can get your project working.
You can easily solve this problem: if the name of your project directory, where manage.py is in, is "RalphPortfolio", you need to make the following correction in settings.py:
'DIRS': ['RalphPortfolio/templates'],
add the full path to the template dirs
for example if your path is /home/some_url/your_project/templates
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['/home/some_url/your_project_name/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',
],
},
},
]
it's because the path is not clear well and it will not find it.

After installing jinja2 TemplateDoesNotExist at /admin/

I have installed jinja2 and after that 'DIRS' stopped working(I have to include them by hand).
Changing 'APP_DIRS' doesn`t help
templates look like that:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'APP_DIRS': False,
'DIRS': ['main/templates', 'shop/templates'],
'OPTIONS': {
'environment': 'django_test.create_jinjia_env.environment',
'autoescape': True,
'auto_reload': DEBUG,
'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 don`t include templates into DIRS it throws the same error
Didn`t find the questions like that. Thanks in advance!
The Django admin app does not come with Jinja templates. If you wish to use Jinja and the admin app, you need to include both engines in your TEMPLATES setting:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True, # This allows Django to find the templates in the admin app
'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.jinja2.Jinja2',
# The rest of your Jinja2 settings.
},
Secondly, when APP_DIRS is True, the Jinja2 backend looks for templates in a jinja2 subdirectory. That means you should put your templates in main/jinja2 and shop/jinja2 instead of main/templates and shop/templates.

Categories