Reputation: 21
I tried to use render to show the HTML edited on PyCharm, but when I entered the address: 127.0.0.1:8000/index/
, the following TemplateDoesNotExist
exception appeared:
TemplateDoesNotExist at /index/ index.html Request Method: GET Request URL: http://127.0.0.1:8000/index/ Django Version: 1.11.1 Exception Type: TemplateDoesNotExist Exception Value:index.html Exception Location: D:\python3.6.1\lib\site-packages\django\template\loader.py in get_template, line 25 Python Executable: D:\python3.6.1\python.exe Python Version: 3.6.1 Python Path:
['C:\Users\Administrator\guest', 'D:\python3.6.1\python36.zip', 'D:\python3.6.1\DLLs', 'D:\python3.6.1\lib', 'D:\python3.6.1', 'D:\python3.6.1\lib\site-packages'] Server time: Fri, 2 Jun 2017 03:30:58 +0000`TemplateDoesNotExist at /index/
settings.py:
ROOT_URLCONF = 'guest.urls'
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',
],
},
},
]
views.py:
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request,"index.html")
Screenshot of PyCharm:
I have tried some methods provided on StackOverflow, but they do not work. Is this exception caused by the wrong dirs? How can I deal with such situation?
Upvotes: 2
Views: 6462
Reputation: 21
I have the same issue. It was mainly caused if you didn't install your app in "settings.py" file
Just do this in your settings.py file and your error will be gone
INSTALLED_APPS = [
'<app name>.apps.<app name>Config',
]
In the place of paste your "app name"
Upvotes: 1
Reputation: 11615
Look at this:
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',
],
},
},
]
Specifically
'DIRS': [],
You need to tell Django where to look for your templates. If you have a templates folder in your main project root. Then, add
os.path.join(BASE_DIR, 'templates')
This assumes your project has this structure:
root
app1
...
templates/
what we're referencing with os.path.join(BASE_DIR, "templates")
....
static/
what we're referencing with STATIC_ROOT = os.path.join(BASE_DIR, "static")
...
If you are placing templates inside of your apps. There are two things.
Your app inside your project will need this structure for the template folder
app name (folder)
templates (folder)
app name (folder)
(template files) e.g. - "base.html"
You will need to add this line
os.path.join(BASE_DIR, APP NAME, "templates")
Addendum: Undoubtedly you will run into this problem as well... so let's go ahead and cover it.
If you are extending / including templates which are in an app (not in the base template folder) you will need to reference where they are located:
Example: I have a template in "myapp/templates/myapp/template.html" and I want to include it.
Then, I'd do {% include "myapp/template.html" %}
or if extending {% extend "myapp/template.html" %}
. If they are in the base template folder then you can just reference them directly as "template.html"
Upvotes: 9