Django TemplateDoesNotExist - can not render the template - django

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'

Related

How can I fix django error? "template does not exist"

I started Django few days ago and I am stuck now.
I got error like this.
django.template.exceptions.TemplateDoesNotExist: home.html
but I don't know how to fix it.
views.py
from django.shortcuts import render
from django.http import HttpResponse
from .models import ToDoList, Item
def index(response, id):
ls = ToDoList.objects.get(id=id)
return render(response, "main/base.html", {"name":ls.name})
def home(response):
return render(response, "main/home.html", {'name':'test'})
base.html
<html> <head> <title>Website</title> </head> <body> <p>{{name}}</p> </body> </html>
home.html
{% extends 'main/base.html' %}
Both html files are in file named templates
I thought template might have something to do with it and checked it.
setting.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',
],
},
},
]
Does someone help me out?
Just add your root template folder name to the DIRS value.
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",
],
},
},
]
You have to have both files in folder: <project_name>/<app_name>/main/, but you probably put it somewhere else.
Additionally, it's good practice to add this value to TEMPLATES["DIRS"]:
"DIRS": [os.path.join(PROJECT_DIR, "templates")],
and then create folder "templates" inside every app you have and store templates inside (move there your whole folder "main" with proper templates)
do this:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR,"templates",
os.path.join(BASE_DIR,"appname","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',
],
},
},
]

why TemplateDoesNotExist at / index.html

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.

Django how to pass context or model objects in base.html without pass views in url

I know I can pass context using somethings like this in base.html
class MyViews(request):
#my code.....
context= {#my context}
return render(request, 'base.html', context)
then pass the views in url. But is there any to pass context in base.html without using url?? any idea?? I also tried this
'context_processors': [
...
# add a context processor
'my_app.context_processor.my_views',
], #getting this error No module named 'contact.context_processor'
You can use context_pro.py it will send context objects to all html files but I dont recommend it. because context pro send contexes all html files if you want to use. first open context_pro.py file
from .models import *
def funcName(request):
some = Some.objects.all()
anther_one = Another.objects.all().order_by('-date')[:16]
context = {
'some':some,
'anther_one':anther_one,
}
return context
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'yourApp.context_pro.funcName',
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

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 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.