2

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:

enter image description here

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?

tuomastik
  • 4,559
  • 5
  • 36
  • 48
Wendy.Wang
  • 21
  • 1
  • 4
  • _caused by the wrong dirs_ ... you don't have ANY dirs: `'DIRS': []` – John Gordon Jun 02 '17 at 03:45
  • Have you done `collectstatics` ?Have you set static path in `settings.py` file?Have you put all the templates in static/templates folder? – Piyush S. Wanare Jun 02 '17 at 06:05
  • 2
    @PiyushS.Wanare literally every part of your comment is wrong. Why would collectstatic have anything to do with templates? Why would templates be in the static directory? – Daniel Roseman Jun 02 '17 at 07:56

2 Answers2

9

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"

Pythonista
  • 11,377
  • 2
  • 31
  • 50
  • Thanks a lot for your patiently explaining,well my templates(folder) was set under my app 'sign',I trid to add the app name 'sign': – Wendy.Wang Jun 02 '17 at 05:27
  • Thanks a lot for your patiently explaining,well ,my templates(folder) was set under my app 'sign',I trid to add the app name 'sign': 'DIRS': [os.path.join(BASE_DIR,sign,"templates")],but another exception occurs, NameError which meaned the name 'sign' did not be defined. I am so sorry I kown very little about the python,but I do really want to come over what I am facing with right now. – Wendy.Wang Jun 02 '17 at 05:35
  • It's not a variable it's a string... Use quotes. Not to sound harsh but you shouldn't be trying to use Django if you don't know this. This is elementary Python and Django is advanced Python. At the very least go through a few tutorials it'll save you some headaches. – Pythonista Jun 02 '17 at 05:37
  • WOW!!!It worked! Thanks a lot ,yeah,I was following a textbook from one of my favorite test engineers ,when I follow the book,I encounter such problem..Aha,thanks again! May I repeat your reasons for my faults? You meaned that the form of the name of my app is string ,so I need to add''> – Wendy.Wang Jun 02 '17 at 06:03
1

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"