How to solve TemplateDoesNotExist at /account - django

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

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

Templates does not exist

How to specify a path to base_site.html that would have views the blog saw it.
enter image description here
You have to do 2 things
Edit the templates settings to include templates folder
Call the template by ‘admin/base_site.html’
First of all add this in your settings.py
'DIRS': [os.path.join(BASE_DIR / "templates")]
For Example:
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',
],
},
},
]
Now change the path or just paste this, and let me know..
return render(request, "admin/base_site.html", {"tasks":tasks})

TemplateDoesNotExit error when deploying project on heroku

I am trying to deploy my project on heroku but I am stumbling on the above error. Everything works fine when i run the code locally. Below are my setting and views file
template settings
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',
],
},
},
]
views of my Home app
def Home(request):
return render(request,'Home/index.html')
Try to use this ( In 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',
'django.template.context_processors.media',
],
},
},
]
Thanks for your suggestion. I solved the error by renaming my templates from 'index.html' to 'main.html'

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.

Adding template path Django

I Have been reading about templates in the django book, the author has suggested to add template path in the setting.py variable TEMPLATE_DIRS but my setting.py has no `TEMPLATE_DIRS'(I understand why is it so)and I could find the below
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(os.path.dirname(__file__), ’templates’).replace(’\\’,’/] #1
'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',
],
},
},
]
1)Does the above change I have madein the settings.py lead to me in the correct direction or is there any better way to do it? I wanna make myself sure before I put my self into others and end up in mess.
TEMPLATE_DIRS variable is deprecated since Django 1.8 (https://docs.djangoproject.com/en/1.9/ref/settings/#template-dirs).
Your setup is fine, but you can clean it up a bit, for example:
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
os.path.join(BASE_DIR, 'templates', 'some_other_dir'),
# other paths
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.template.context_processors.media',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]