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

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

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'

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

How to customize template in django-oscar?

i want to customize django-oscar template according to my requirement , when i modified the checkout page, i had been encoutred with this error :
TemplateSyntaxError at /checkout/thank-you/
Invalid block tag on line 132: 'oscar_thumbnail', expected 'endwith'. Did you forget to register or load this tag?
this my settings.py :
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates'),
OSCAR_MAIN_TEMPLATE_DIR
]
,
# 'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.i18n',
'django.contrib.messages.context_processors.messages',
'oscar.apps.search.context_processors.search_form',
'oscar.apps.promotions.context_processors.promotions',
'oscar.apps.checkout.context_processors.checkout',
'oscar.apps.customer.notifications.context_processors.notifications',
'oscar.core.context_processors.metadata',
],
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
},
},
]
how i can register the templatetag into my custom template in django-oscar?

Context Processors are not working with Jinja2 in Django

Context Processors are not working with Jinja2(Ver 2.10) in Django(Ver 2.0.5). This is what i have done. Created a context processor as follows:
def test_con_proc(request):
return {
'test_con_proc': "Testing Context Processors",
}
And, called it in my template using this:
{{ test_con_proc }}
Also, added this to settings.py file like this:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'DIRS': [....)
],
'APP_DIRS': True,
'OPTIONS': {
'environment': '....jinja2.environment',
},
},
{
'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',
'dashboard.context_processors.test_con_proc',
],
},
},
]
So, What't the proper solution for resolving using context processors with Jinja2 in Django?
You need to install django-jinja then:
change: 'BACKEND': 'django.template.backends.jinja2.Jinja2',
to "BACKEND": "django_jinja.backend.Jinja2",
After that move your context processor to jinja OPTIONS['context_processors']
So your settings should look similar to this:
TEMPLATES = [
{
'BACKEND": "django_jinja.backend.Jinja2',
'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',
'dashboard.context_processors.test_con_proc',
]
}
},
]

Can't use Jinja elements on Page

I am working on Django and new to Jinja templates. I am able to print variable from the context but can't use other features of Jinja.
When I do {{ 1+1 }} on the page . it shows:
Could not parse the remainder: '+1' from '1+1'
I am trying to generate a random no. by {{ range(1, 51) | random }} ,As by this answer. but it throws error as:
Could not parse some characters: range|(1, 51)| | random
In settings.py :
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'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',
],
},
},
]
The problem is you didn't activate Jinja2 template in your Django project. Please set the Template Engine properly.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
...
TEMPLATES = [{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'BACKEND': 'django.template.backends.django.DjangoTemplates', # remove this line
'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',
],
},
}]
In line 3 you override key BACKEND with Django Template Engine.