why TemplateDoesNotExist at / index.html - django

The error message is:
TemplateDoesNotExist at /
index.html
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 4.0.2
Exception Type: TemplateDoesNotExist
Exception Value:
index.html
Exception Location: C:\Python310\lib\site-packages\django\template\loader.py, line 19, in get_template
Python Executable: C:\Python310\python.exe
Python Version: 3.10.2
Python Path:
['C:\\Users\\Asus\\Desktop\\31-5-22 Django\\wscubetech_firstproject',
'C:\\Python310\\python310.zip',
'C:\\Python310\\DLLs',
'C:\\Python310\\lib',
'C:\\Python310',
'C:\\Python310\\lib\\site-packages']
Server time: Sat, 04 Jun 2022 17:38:04 +0000
Here is my code:
views.py
from django.http import HttpResponse
from django.shortcuts import render
def aboutUs(request):
return HttpResponse('hello BD')
def homePage(request):
return render(request,"index.html")
urls.py
from django.contrib import admin
from django.urls import path
from wscubetech_firstproject import views
urlpatterns = [
path('admin/', admin.site.urls),
path('about-us/',views.aboutUs),
path('',views.homePage),
]
settings.py
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',
],
},
},
]
Project structure:

I think you just need to change the TEMPLATES setting a bit.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR/"templates"], # right here
'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',
],
},
},
BASE_DIR object is pathlib.Path instance and the slash operator helps create child paths, similarly to os.path.join().
For more information you can check out the pathlib documentation.

Related

Django TemplateDoesNotExist - can not render the template

I am new to django and I am using version 4.1.2. I have created an app with the following structure
I have configured the app ( 'home') template in the primary setting file like the following. but still, I am getting the error
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR/'home', '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',
],
},
},
]
here is my view code
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return render(request, ' home/welcome.html' , {})
Remove the space in the template path: ' home/welcome.html' have to be 'home/welcome.html'

TemplateDoesNotExist at /home/ entries/index.html, entries/entry_list.html

I need an extra pair of eyes, I just cannot see the mistake, below you can see all the necessary files, I think the error comes from there
views.py
from django.shortcuts import render
from django.views.generic import ListView
from .models import Entry
class HomeView(ListView):
model = Entry
template_name='entries/index.html'
urls.py
from .views import HomeView
from django.urls import path
urlpatterns = [
path('home/', HomeView.as_view(), name='blog-home')
]
base urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('entries.urls'))
]
templates settings
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',
],
},
},
]
you need to add the templates dirs in settings.py , change this
'DIRS': [],
to this
'DIRS': ['templates'],
in the settings.py file , template settings

How to solve TemplateDoesNotExist at /account

I am getting TemplateDoesNotExist at /account issues in my website, all code is working perfect on my localhost but this issue is coming on server, please let me know where is the mistake.
Here is my urls.py file...
app_name='account'
urlpatterns = [
path('account', views.accounts, name='useraccount'),
]
here is my views.py file...
def accounts(request):
return render(request, 'account/account.html')
my account.html file path is this....account(app name)/templates/account/account.html
and this is link...
<li>
Account
</li>
here is the value of TEMPLATES in setting.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',
'mainpage.context_processors.context_categories',
],
},
},
]

Django template not found although it it exists in templates folder

Django version 2.2.6
I have the following folder structure and was expecting Django to find index.html based as it is placed in the default location under templates.
Am I doing something wrong here? Did Django stop looking up the template paths by default?
-app
|
settings.py
urls.py
...
templates
|
base.html
index.html
views.py
from django.shortcuts import render
# Create your views here.
def home_view(request):
return render(request, 'index.html')
urls.py
from django.contrib import admin
from django.urls import path
from .views import home_view
urlpatterns = [
path('', home_view, name='index'),
path('admin/', admin.site.urls),
]
You need to set the path of your templates folder in settings.py.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Leave DIRS blank and try again.
you are importing the home view page wrongly in the urls.py

Django context processors model not found

I am trying to implement a context_processor in Django via a custom app. I am failing due to Module not found error.
My app name is brand.
My context processer looks like this:
from .models import ContactDetail
def contact(request):
contact = ContactDetail.objects.first()
return { 'contact' : contact }
My app is included in installed apps and the model has been migrated.
I have included my context processor into the context_processors list as per documentation:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'mysite', 'templates'),],
'OPTIONS': {
'context_processors': [
'brand.context_processors.contact',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.i18n',
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.template.context_processors.media',
'django.template.context_processors.csrf',
'django.template.context_processors.tz',
'sekizai.context_processors.sekizai',
'django.template.context_processors.static',
'cms.context_processors.cms_settings',
],
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'django.template.loaders.eggs.Loader'
],
},
},
]
The Error I am getting is:
Django Version: 1.11.16
Exception Type: ModuleNotFoundError
Exception Value: No module named 'brand.context_processors'
I have tried searching for solutions but most of the questions are for previous versions of django ( <= 1.8 ) I am using 1.11.16.
I have read through the docs and it is unclear what I am missing.
Any help is apreciated.
Make sure that you named that file context_processors.py and placed in myapp directory.