Reputation: 81
I am new to Django , I'm working on system Windows 10 , Django version 3.0.1 ,python 3.6 I tried to use all the relevant solutions provided for this problem , but unfortunately none of those resolve my problem . I made a folder named templates in myapp and "index.html" inside it, I write some lines of code in my views.py file as
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
def index(request):
template = loader.get_template('index.html')
return HttpResponse(template.render())
here is my settings.py
BASE_DIR = (os.path.dirname(os.path.abspath(__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',
],
},
},
]
**Traceback error **
Template loader postmortem
Django tried loading these templates, in this order:
Using engine django:
* django.template.loaders.filesystem.Loader: E:\djangoLearning\myproject\myapp\templates\index.html (Source does not exist)
* django.template.loaders.app_directories.Loader: E:\djangoLearning\djangoenv\lib\site-packages\django\contrib\admin\templates\index.html (Source does not exist)
* django.template.loaders.app_directories.Loader: E:\djangoLearning\djangoenv\lib\site-packages\django\contrib\auth\templates\index.html (Source does not exist)
* django.template.loaders.app_directories.Loader: E:\djangoLearning\myproject\myapp\templates\index.html (Source does not exist)
Traceback (most recent call last):
File "E:\djangoLearning\djangoenv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "E:\djangoLearning\djangoenv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "E:\djangoLearning\djangoenv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "E:\djangoLearning\myproject\myapp\views.py", line 9, in index
template = loader.get_template('index.html')
File "E:\djangoLearning\djangoenv\lib\site-packages\django\template\loader.py", line 19, in get_template
raise TemplateDoesNotExist(template_name, chain=chain)
Exception Type: TemplateDoesNotExist at /index/
Exception Value: index.html
workng directory
myproject
myapp
templates
index.html
Upvotes: 0
Views: 2076
Reputation: 1105
Step:-1) Go to your project settings.py file
Add the following template list of configuration:
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',
],
},
},
]
Step:--2) Go to your app >> Create templates folder >> create a folder by your_app_name again>> create your template file like: index.html
Your_APP_Name
|-templates
|-Your_APP_Name
|-index.html
Say, you have a blog app. So the structure will be like:
blog
|-templates
|-blog
|-index.html
Step:-3) Now you may call the template file precisely in render method. So the view will be look like:
from django.shortcuts import render, redirect
from django.http import HttpResponse
def index(request):
return render(request,'your_app_name/index.html')
Upvotes: 2