TemplateDoesNotExist at certain location

Question:

My code was working perfectly fine until yesterday. I had deployed the app on Heroku and it went well. I wanted to work on the security of the app, so today I deployed some code changes directly to Heroku. When it didn’t run perfectly, I reverted the changes and by mistake, I copied the settings.py from my production server to development server. Now the deployed code is running perfectly, though my development server is unable to find the templates!

When I realized that I’m using the production settings.py, I made the necessary changes. Debug = True, removed security headers and made sure the middlewares were the default ones.

Initially, I kept getting the below error while accessing https://localhost:8000

You're accessing the development server over HTTPS, but it only supports HTTP.

I then changed my URL to simply 127.0.0.1:8000, and then I started getting the below error

raise TemplateDoesNotExist(template_name, chain=chain)
django.template.exceptions.TemplateDoesNotExist: my_form/index.html
[11/Jun/2019 23:25:49] "GET / HTTP/1.1" 500 88781

I’ve even tried to start the project from scratch and even cleared my cache from the browser.

Project Structure:

├───my_server
│   ├───my_form
│   │   ├───migrations
│   │   │   └───__pycache__
│   │   ├───static
│   │   │   └───raffle_form
│   │   │       ├───images
│   │   │       └───styles
│   │   ├───templates
│   │   │   └───my_form
│   │   └───__pycache__
│   └───my_server
│       └───__pycache__

Here is the urls.py from my_server:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('my_form.urls')),
]

Here is the urls.py from my_form:

from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]

Here is the views.py from my_form app:

from django.shortcuts import render

# Create your views here.
def index(request):
    return render(request, 'my_form/index.html')

I’ve created a templates folder within my_form app. Within templates, I’ve my_form folder which contains the index.html files

I’m pretty sure nothing is wrong with the setup since it was already working. I believe the trick would be either to flush some files or update something in my settings.py

I would have shared my settings.py if there was something to show, but its just the default one.

Your thoughts?

Asked By: Ronnie

||

Answers:

After almost a two day bout with the problem, I decided to go back even further, and reinstall Django packages. For some unknown reason, it solved my issue. However, I’m still not sure if any package/module/definition was missing, why it wasn’t reported in the logs?!

To anyone struggling with the same issue, I would suggest going back one step at a time.

  • Check your app’s views.py and urls.py.
  • Check your projects urls.py.
  • Check if you’ve registered the app in the settings.py.
  • Check C:UsersAppDataLocalProgramsPythonPython36Libsite-packagesdjangotemplate directory for relevant files`

As for other experts, who might have some light to throw in here, please feel free to comment/post.

Answered By: Ronnie

If someone ever experience this again, please take a look at settings.py and ensure the INSTALLED_APPS contains the app you want to render the template.

settings.py

Answered By: Bee Bee Wijaya
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.