Reputation: 5450
I have the following view in my Django project:
import os
from django.shortcuts import render
from JumboDjango import settings
# Create your views here.
def index(request):
filename = os.path.join(settings.BASE_DIR, 'frontend/source/public/index.html')
return render(request, filename)
But, when it is called, it raises the following exception:
Internal Server Error: /
Traceback (most recent call last):
File "/Users/hugovillalobos/Documents/Code/JumboDjangoProject/JumboDjangoVenv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/hugovillalobos/Documents/Code/JumboDjangoProject/JumboDjangoVenv/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/hugovillalobos/Documents/Code/JumboDjangoProject/JumboDjangoVenv/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/hugovillalobos/anaconda3/lib/python3.7/contextlib.py", line 74, in inner
return func(*args, **kwds)
File "/Users/hugovillalobos/Documents/Code/JumboDjangoProject/JumboDjango/frontend/views.py", line 8, in index
return render(request, filename)
File "/Users/hugovillalobos/Documents/Code/JumboDjangoProject/JumboDjangoVenv/lib/python3.7/site-packages/django/shortcuts.py", line 36, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "/Users/hugovillalobos/Documents/Code/JumboDjangoProject/JumboDjangoVenv/lib/python3.7/site-packages/django/template/loader.py", line 61, in render_to_string
template = get_template(template_name, using=using)
File "/Users/hugovillalobos/Documents/Code/JumboDjangoProject/JumboDjangoVenv/lib/python3.7/site-packages/django/template/loader.py", line 19, in get_template
raise TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: /Users/hugovillalobos/Documents/Code/JumboDjangoProject/JumboDjango/frontend/source/public/index.html
[26/Sep/2019 02:51:04] "GET / HTTP/1.1" 500 79956
You can see in the following image that /Users/hugovillalobos/Documents/Code/JumboDjangoProject/JumboDjango/frontend/source/public/index.html
actually exists:
I don't know if I am missing any Django configuration, or something.
EDIT
This is my templates settings:
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',
],
},
},
]
Upvotes: 3
Views: 1177
Reputation: 711
First of all, You should always keep html files and static files (css, js, json) seperate.
this is good approch for me which I always use. For HTML files, define template directory in project folder.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
.
.
.
},
]
This templates
folder should be in project directory. inside this folder, you can create folder for your apps or anything. Like current case, a public
folder inside templates
and html file inside it. You can access this file like this:
return render(request, 'public/index.html')
---Edit------
For condition in this question, if public is folder where we will have all html files and JumboDjango is project directory, then:
settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'frontend/source/public/')],
.
.
.
},
]
views.py
return render(request, 'index.html')
if we create another folder inside public like folder1, then
return render(request, 'folder1/index.html')
But separate folder for static files is necessary.
Upvotes: 2
Reputation: 3697
You need to add the directory to your TEMPLATES
settings under the DIRS
key.
It allows the Django template engine to look for template files in that folder.
For instance, if your template directory is /Users/hugovillalobos/Documents/Code/JumboDjangoProject/JumboDjango/frontend/source
So you need to add it as below to DIRS
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
"/Users/hugovillalobos/Documents/Code/JumboDjangoProject/JumboDjango/frontend/source"
],
...
...
},
]
You need to render your template by giving the relative path to the render
method.
import os
from django.shortcuts import render
from JumboDjango import settings
# Create your views here.
def index(request):
return render(request, "public/index.html") # Add a relative path to your template folder here.
Upvotes: 0