This has been posted a few times that I’ve seen with no good answer. Hoping to get one here. I’m getting a TemplateNotFound error in Django.
Error:
TemplateDoesNotExist at /empreview/people_choice.html
empreview/people_choice.html
Request Method: GET
Request URL: http://eso-qppapp-testing.azurewebsites.net/empreview/people_choice.html
Django Version: 4.1.5
Exception Type: TemplateDoesNotExist
Exception Value:
empreview/people_choice.html
Exception Location: /tmp/8db29bc229d2f6d/antenv/lib/python3.10/site-packages/django/template/loader.py, line 19, in get_template
Raised during: EmpReview.views.search_person
Python Executable: /tmp/8db29bc229d2f6d/antenv/bin/python
Python Version: 3.10.9
Python Path:
['/tmp/8db29bc229d2f6d',
'/tmp/8db29bc229d2f6d/antenv/bin',
'/opt/startup/app_logs',
'/tmp/8db29bc229d2f6d/antenv/lib/python3.10/site-packages',
'/opt/python/3.10.9/lib/python310.zip',
'/opt/python/3.10.9/lib/python3.10',
'/opt/python/3.10.9/lib/python3.10/lib-dynload',
'/opt/python/3.10.9/lib/python3.10/site-packages',
'/opt/python/3.10.9/lib/python3.10/site-packages/viztracer/attach_process']
Server time: Mon, 20 Mar 2023 22:33:29 -0500
Template-loader postmortem
Django tried loading these templates, in this order:
Using engine django:
django.template.loaders.filesystem.Loader: /tmp/8db29bc229d2f6d/EmpReview/templates/empreview/people_choice.html (Source does not exist)
django.template.loaders.app_directories.Loader: /tmp/8db29bc229d2f6d/antenv/lib/python3.10/site-packages/django/contrib/admin/templates/empreview/people_choice.html (Source does not exist)
django.template.loaders.app_directories.Loader: /tmp/8db29bc229d2f6d/antenv/lib/python3.10/site-packages/django/contrib/auth/templates/empreview/people_choice.html (Source does not exist)
django.template.loaders.app_directories.Loader: /tmp/8db29bc229d2f6d/EmpReview/templates/empreview/people_choice.html (Source does not exist)
urls.py:
urlpatterns = [
path('', views.index, name='index'),
path('people/', views.PersonView.as_view(), name='people'),
path('person/<uuid:pk>', views.PersonDetail, name='person-detail'),
path('review/<uuid:pk>', views.ReviewDetailView.as_view(), name='review-detail'),
path('approval/<uuid:pk>', views.ApprovalDetailView.as_view(), name='approval-detail'),
path('reviews/my_created_reviews.html', views.ReviewsByRequester.as_view(), name='my_created_reviews'),
path('people_choice.html', views.search_person, name='people_choice'),
path('success.html', views.success, name='success'),
path('name-autocomplete/', views.NameAutoComplete.as_view(), name='name-autocomplete'),
path('get_or_create/<int:id>', views.get_or_create, name='get-or-create')
]
view in views.py in question:
def search_person(request):
all_users = {}
results = {}
# print(currentUsers)
if 'name' in request.GET:
name = request.GET['name'].capitalize()
all_users = Employee.objects.all().filter(user__first_name=name) | Employee.objects.all().filter(user__last_name=name)
return render(request, 'empreview/people_choice.html', {"all_users": all_users})
settings.py (template portion):
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR.joinpath('EmpReview/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',
],
},
},
]
Now, the part that seems to cause confusion and the most infuriatingly ignored part of almost every one of the answers I’ve looked at here, this is deployed on azure. Here is a screenshot of the file structure straight from Azure:
You can see the folder structure along with the listing of files in the directory where ‘people_choice.html’ clearly resides. Why isn’t Django able to recognize that there is a template in the exact place that it is looking on lines 1 and 4 of the postmortem? I would like to note that if I run this project on localhost, it has no issue finding the template whatsoever.