I am having trouble with Django routing to find templates located at the app-level. That is, templates at the app-level directory are not rendered unless I put them in the root project-level directory. But the problem with this architecture for me is that, if I have two or more apps all having a file named home.html how do I resolve that from the templates folder at the project-level so that they won't be confliction.
Error message
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: C:\Users\HP\Desktop\DEV\PYTHON\DEVELOPMENT\WILLIAM-VINCENT-PROJECT\django-begin\templates\index.html (Source does not exist)
I was expecting Django to search the /templates/posts and /templates/pages folders to fetch the files. It is rather trying to fetch the files directly from the templates folder. If I were to put the HTML files directly into the /templates/ folder then that would conflicts since apps have similar file names
Project directory structure
|__my-project
|__manage.py
|__pages
|__posts
|__templates
|__pages
| |__home.html
| |__about.html
|__posts
|__home.html
|__about.html
I want each app to have its own home.html and about.html
INSTALLED_APPS = [
'posts.apps.PostsConfig',
'pages.apps.PagesConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(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',
],
},
},
]
Your templates need to be saved in the following way:
ProjectName/AppName/Templates/AppName/Template.htmlSo if your project was called
DjangoProjectand your app was calledDjangoAppit would be:DjangoProject/DjangoApp/Templates/DjangoApp/Template.htmlThis is the same for any app you start within your project.
EDIT:
Currently you have a
templatesfolder within your project directory. That is wrong.You need a
templatesfolder for each app within that apps directory.So
my-project/pages/templates/pagesandmy-project/posts/templates/postsNOT
my-project/templates/