how to fix template does not exist in Django?
Question:
I am a beginner in the Django framework. I created my project created my app and test it, it work fine till I decided to add a template. I don’t know where the error is coming from because I follow what Django docs say by creating folder name templates in your app folder creating a folder with your app name and lastly creating the HTML file in the folder.
NOTE: other routes are working fine except the template
Please view the screenshot of my file structure and error below.
File Structure
ERROR
TemplateDoesNotExist at /blog/
Blog/index
Request Method: GET
Request URL: http://127.0.0.1:8000/blog/
Django Version: 4.0.1
Exception Type: TemplateDoesNotExist
Exception Value:
Blog/index
Exception Location: C:Python39libsite-packagesdjangotemplateloader.py, line 19, in get_template
Python Executable: C:Python39python.exe
Python Version: 3.9.4
Python Path:
['C:\Users\Maxwell\Desktop\Django\WebApp',
'C:\Python39\python39.zip',
'C:\Python39\DLLs',
'C:\Python39\lib',
'C:\Python39',
'C:\Users\Maxwell\AppData\Roaming\Python\Python39\site-packages',
'C:\Python39\lib\site-packages',
'C:\Python39\lib\site-packages\win32',
'C:\Python39\lib\site-packages\win32\lib',
'C:\Python39\lib\site-packages\Pythonwin']
Server time: Sun, 23 Jan 2022 12:04:18 +0000
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: C:UsersMaxwellDesktopDjangoWebAppBlogtemplates Blogindex (Source does not exist)
django.template.loaders.app_directories.Loader: C:Python39libsite-packagesdjangocontribadmintemplates Blogindex (Source does not exist)
django.template.loaders.app_directories.Loader: C:Python39libsite-packagesdjangocontribauthtemplates Blogindex (Source does not exist)
App/views.py
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request,name):
return HttpResponse(f'Hello {name}')
def html(request):
return render(request,' Blog/index.html')
Answers:
It seems that you render the template with Blog/index
, but you need to specify the entire file name, so Blog/index.html
and without a leading (or trailing) space:
def html(request):
return render(request, 'Blog/index.html')
if @Willem van Onsem answer didn’t work
create a templates folder in the main directory ‘in the same level as WebApp’
then inside it create folders for each app
in your settings.py you will find TEMPLATES option
TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR,'templates')],#add this line
...
]
and when you want to use a template
return render(request,"template_folder/template_name.html")
I faced this situation too, and went through most of the answers here talking about the path, and editing the ‘templates’ function in settings.py.
In the end, it was because I forgot to add my app to the installed apps section in the settings.py file. I saw this in the docs at https://docs.djangoproject.com/en/4.0/intro/tutorial03/#write-views-that-actually-do-something
I was also facing same issue, as main cause of the issue is that you forget
to mentioned your app(Application) in setting.py file
As I can see the path not containing your project its taking default as installed.
I am a beginner in the Django framework. I created my project created my app and test it, it work fine till I decided to add a template. I don’t know where the error is coming from because I follow what Django docs say by creating folder name templates in your app folder creating a folder with your app name and lastly creating the HTML file in the folder.
NOTE: other routes are working fine except the template
Please view the screenshot of my file structure and error below.
File Structure
ERROR
TemplateDoesNotExist at /blog/
Blog/index
Request Method: GET
Request URL: http://127.0.0.1:8000/blog/
Django Version: 4.0.1
Exception Type: TemplateDoesNotExist
Exception Value:
Blog/index
Exception Location: C:Python39libsite-packagesdjangotemplateloader.py, line 19, in get_template
Python Executable: C:Python39python.exe
Python Version: 3.9.4
Python Path:
['C:\Users\Maxwell\Desktop\Django\WebApp',
'C:\Python39\python39.zip',
'C:\Python39\DLLs',
'C:\Python39\lib',
'C:\Python39',
'C:\Users\Maxwell\AppData\Roaming\Python\Python39\site-packages',
'C:\Python39\lib\site-packages',
'C:\Python39\lib\site-packages\win32',
'C:\Python39\lib\site-packages\win32\lib',
'C:\Python39\lib\site-packages\Pythonwin']
Server time: Sun, 23 Jan 2022 12:04:18 +0000
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.app_directories.Loader: C:UsersMaxwellDesktopDjangoWebAppBlogtemplates Blogindex (Source does not exist)
django.template.loaders.app_directories.Loader: C:Python39libsite-packagesdjangocontribadmintemplates Blogindex (Source does not exist)
django.template.loaders.app_directories.Loader: C:Python39libsite-packagesdjangocontribauthtemplates Blogindex (Source does not exist)
App/views.py
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request,name):
return HttpResponse(f'Hello {name}')
def html(request):
return render(request,' Blog/index.html')
It seems that you render the template with Blog/index
, but you need to specify the entire file name, so Blog/index.html
and without a leading (or trailing) space:
def html(request):
return render(request, 'Blog/index.html')
if @Willem van Onsem answer didn’t work
create a templates folder in the main directory ‘in the same level as WebApp’
then inside it create folders for each app
in your settings.py you will find TEMPLATES option
TEMPLATES = [
{
...
'DIRS': [os.path.join(BASE_DIR,'templates')],#add this line
...
]
and when you want to use a template
return render(request,"template_folder/template_name.html")
I faced this situation too, and went through most of the answers here talking about the path, and editing the ‘templates’ function in settings.py.
In the end, it was because I forgot to add my app to the installed apps section in the settings.py file. I saw this in the docs at https://docs.djangoproject.com/en/4.0/intro/tutorial03/#write-views-that-actually-do-something
I was also facing same issue, as main cause of the issue is that you forget
to mentioned your app(Application) in setting.py file
As I can see the path not containing your project its taking default as installed.