Please help with Project-Level 'templates directory.'

I am working on chapter 4 of Django for Beginners by M Vincent. He suggests we create a project-level “templates” directory. Inside this directory should be the home.html. From my inexperienced point of view I think I have done just that. However when I run the development server I get a TemplateDoesNotExist error. What should I be doing.
This is my settings.py file

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [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",
            ],
        },
    },
]

The templates directory is on the same level as the django_project and the pages app

This is the traceback that I get

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/

Django Version: 5.1.4
Python Version: 3.12.3
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'pages']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Template loader postmortem
Django tried loading these templates, in this order:

Using engine django:
    * django.template.loaders.filesystem.Loader: C:\Users\ZACKAMATA\Documents\Learn Python\Django\LearnDjango\company\templates\home.htl (Source does not exist)
    * django.template.loaders.app_directories.Loader: C:\Users\ZACKAMATA\Documents\Learn Python\Django\LearnDjango\company\.venvcompany\Lib\site-packages\django\contrib\admin\templates\home.htl (Source does not exist)
    * django.template.loaders.app_directories.Loader: C:\Users\ZACKAMATA\Documents\Learn Python\Django\LearnDjango\company\.venvcompany\Lib\site-packages\django\contrib\auth\templates\home.htl (Source does not exist)



Traceback (most recent call last):
  File "C:\Users\ZACKAMATA\Documents\Learn Python\Django\LearnDjango\company\.venvcompany\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ZACKAMATA\Documents\Learn Python\Django\LearnDjango\company\.venvcompany\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ZACKAMATA\Documents\Learn Python\Django\LearnDjango\company\pages\views.py", line 7, in home_page_view
    return render(request, "home.htl")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ZACKAMATA\Documents\Learn Python\Django\LearnDjango\company\.venvcompany\Lib\site-packages\django\shortcuts.py", line 25, in render
    content = loader.render_to_string(template_name, context, request, using=using)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ZACKAMATA\Documents\Learn Python\Django\LearnDjango\company\.venvcompany\Lib\site-packages\django\template\loader.py", line 61, in render_to_string
    template = get_template(template_name, using=using)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ZACKAMATA\Documents\Learn Python\Django\LearnDjango\company\.venvcompany\Lib\site-packages\django\template\loader.py", line 19, in get_template
    raise TemplateDoesNotExist(template_name, chain=chain)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Exception Type: TemplateDoesNotExist at /
Exception Value: home.htl

How can I show the VSCode files and folders structure?

What is that error message telling you?

What is the file name of the template file you’re trying to use?

What is the file name that you are trying to reference in your view?

Hi Ken. Tis really sad about the fires in Pacific Palisades LA. Hope you are all right. You in Pennsylvania right? Ok. I really must become more aware. I actually sam the file ending htl and it did not register in my mind. It was when I went and looked at the views.py file that I saw that I had given the wrong file extension. I change it. But then a new error came up. I saw a yellow squiggly warning line under my import command. Under

from django.shortcuts import render

There is a yellow warning line.
When I control click on it it tells me that the the folder/file contains emphasized items

I added # type: ignore and the yellow warning line disappeared

I ran the development server and my home.html file shows up properly in the browser. Thanks

1 Like

you can send a screenshot of the vscode so we can see the file structure.

also the error:

Exception Type: TemplateDoesNotExist at /
Exception Value: home.htl

could be a typo on your view, looks like you have passed home.htl instead of home.html

Please do not suggest sending screenshots. We try to avoid using images here as much as is possible. For directory listings, the text output of a “dir” or “ls” command is much preferable.

1 Like

Yes. Thank you very much.

@KenWhitesell thanks for this tip for using the text output of the “dir” in command prompt for directory listings