TemplateDoesNotExist at /store/home.html

Hi, I’m using Django Version: 4.0.6 and Python Version: 3.10.5 on windows 10
I am getting the “TemplateDoesNotExist” error and trying to resolve it since past 2 days.
I have added the path for templates folder.

‘DIRS’: [os.path.join(BASE_DIR, ‘templates’)],

Because this didn’t work I also created another templates folder inside the app. and I’m still getting the same error.

I have used the following lines in the views.py

return render(request, ‘store/home.html’, {‘products’ : products})

I have recently started learning Django and this is getting frustrating. Please need some help. Below is the error message

Template-loader postmortem

Django tried loading these templates, in this order:

Using engine django:

  • django.template.loaders.filesystem.Loader: D:\ecommerce\store\home.html (Source does not exist)
  • django.template.loaders.filesystem.Loader: D:\templates\store\home.html (Source does not exist)
  • django.template.loaders.app_directories.Loader: D:\ecommerce\venv\lib\site-packages\django\contrib\admin\templates\store\home.html (Source does not exist)
  • django.template.loaders.app_directories.Loader: D:\ecommerce\venv\lib\site-packages\django\contrib\auth\templates\store\home.html (Source does not exist)

The template loader message is telling you where it’s looking.

Where do you have this home.html template?

What is your complete TEMPLATES setting?

In what view does this line of code appear? (Please include the complete path)

Also, when posting code, templates, or error messages here, surround each block of code (or template, or error message) between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep that text properly formatted, which is critical with Python.

1 Like

Thank you for the reply sir,

  1. Initially I had home.html at the Project level (where the templates of all the apps are stored)
    |    |-templates(same level as manage.py, project, app, venv)
    |    |    |-app_name(store)
    |    |    |    |-home.html ```

  Because I was getting the ```Template Does not exist error``` I shifted the ```templates directory``` inside the ```app folder``` as shown below:

```|-app_name(store)
   |    |-templates
   |    |    |-app_name(store)
   |    |    |    |-home.html ```

I also tried having both simultaneously.

 2) Below is the template list in my settings.py file(I thought that was the 'Template Settings' you are referring to.

``` TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],   #I also tried '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',
            ],
        },
    },
] ```

  3) The 'views.py' is in my app named 'store'

```ECOMMERCE
    |    |-core(project_name)
    |    |-store(app_name)
    |    |    |-views.py ```

Below is the content of 'views.py'

from django.shortcuts import render
from .models import Category, Product

def all_products(request):
products = Product.objects.all()
return render(request, ‘store/home.html’, {‘products’ : products})```
Thank you!

It looks like from the diagram that home.html is in the templates directory and not the store/templates directory. (At least your image doesn’t show what’s in the store directories.) What’s in those store directories?

(Side note: The three backtick characters must be lines by themselves, not part of the lines of code.)

Sir, I learnt from an article that there are two ways of letting Django know where it has to look for the template.
a) By creating the ‘templates’ folder inside the particular ‘app directory’ and ensure that the ‘template files’ are placed in a directory with same name as the ‘app name’(inside the ‘templates’ dir). This I have marked as ‘a’ in the image shared.
b) By creating the Project level ‘templates’ directory and inside this directory create sub directory with same name as the concerned ‘App’. Inside the app_named directory lies your ‘template files’. In this image shared below this is marked as ‘b’.

I got error in both the methods so I left them as it is till I get rid of the error. Please correct me if I am wrong in my understanding of what I have mentioned.

‘core’ is the name of my project and ‘store’ is the name of the app I created.

Thank you.

What is your BASE_DIR?

What is your INSTALLED_APPS setting?

What is in your store/apps.py file?

This relates to your APPS_DIR attribute of your TEMPLATES setting. This should work assuming all other related settings are correct. (You have proper contents in your apps.py file and your INSTALLED_APPS is referencing the proper class.)

This is the directory structure searched by the DIRS attribute of TEMPLATES

(Side note on the directory names. They don’t need to match the app in which they’re placed. They can match any installed app name. That’s how an application can override a template originally defined in a different app.)

The problem here is that the image you’ve posted isn’t clear as to what your directory structure actually is. The image leads me to believe that the store directory is in the core directory and not at the same level as it, inside the ECOMMERCE directory.

Verify that your actual directory structure is as described in the post:

d:\ECOMMERCE
d:\ECOMMERCE\core
d:\ECOMMERCE\store
d:\ECOMMERCE\store\templates
d:\ECOMMERCE\store\templates\store
d:\ECOMMERCE\store\templates\store\home.html

and not:

d:\ECOMMERCE
d:\ECOMMERCE\core
d:\ECOMMERCE\core\store
d:\ECOMMERCE\core\store\templates
d:\ECOMMERCE\core\store\templates\store
d:\ECOMMERCE\core\store\templates\store\home.html
1 Like

My “BASE_DIR” is ‘ECOMMERCE’

BASE_DIR = Path(__file__).resolve().parent.parent
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'store',
]
from django.apps import AppConfig

class StoreConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'store'

This is the actual directory structure among the two. Also, I have tried the one below:

d:\ECOMMERCE
d:\ECOMMERCE\core
d:\ECOMMERCE\store
d:\ECOMMERCE\templates
d:\ECOMMERCE\templates\store
d:\ECOMMERCE\store\templates\store\home.html

I think there’s a chance your BASE_DIR isn’t what you think it is.

Add a print statement after the BASE_DIR line to verify it.

(Side note, in most of my projects, I just have 'DIRS': ['templates'] as the setting. But I do have a couple projects using the os.path.join function as well.)

You might also want to verify if the directory name is “ECOMMERCE” or “ecommerce”, and whether you’re running this on a file system where case-sensitivity is important.

Either way, there’s something really odd going on here.

It may be helpful at this point if you were to post your complete settings file.

Also, how are you running this? Are you running this from the command line or in PowerShell? Is your venv active when you run this?

I got the following output using print statement:

D:\ecommerce

The directory I created was “ECOMMERCE” but my “BASE_DIR” shows “ecommerce". I’m using Windows 10 O.S which uses the default file system NTFS.

I’m using VSCode and tried running on both command line and in PowerShell options available.

from pathlib import Path
import os

BASE_DIR = Path(__file__).resolve().parent.parent

SECRET_KEY = 'django-insecure-6yc5(cv#n-)ihv*#k*g_)7#@92r!f5v)m(&k8%o**#j_b46yd9'

DEBUG = True

ALLOWED_HOSTS = ['127.0.0.1']

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'store',
]

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',
]

ROOT_URLCONF = 'core.urls'

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',
            ],
        },
    },
]

WSGI_APPLICATION = 'core.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True

STATIC_URL = 'static/'

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')

Thank you.

Ok, this definitely isn’t right. You’re creating a list with two options - BASE_DIR and ‘/templates’, neither one is the location of your template file.

It should either be ['templates'] or [os.path.join(BASE_DIR, 'templates')]

Try running it from a separate command line window where you can verify that the correct directory is the current directory and that the right virtual env is active. There may be a configuration issue with your VS Code launch.json file.

Thank you sir, It is working now.
The mistake was that ‘store’ was not containing the ‘home.html’. They were at the same level(meaning in the same folder).

Now I changed it as follows:

sample2

Thank You for your valuable time Sir, I have learnt a lot from you. I hope I will be able to pass on the knowledge.
Have a Nice Time.!!

Earlier it was as below:

sample