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.
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.
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)
| | | |-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 = [
'DIRS': [os.path.join(BASE_DIR, 'templates')], #I also tried 'DIRS' : [BASE_DIR, '/templates]
3) The 'views.py' is in my app named 'store'
| | |-views.py ```
Below is the content of 'views.py'
from django.shortcuts import render
from .models import Category, Product
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.
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:
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.