TemplateDoesNotExist Error - Book Tutorial

I am following the tutorial in the book Django 4 for the Impatient and I’m getting TemplateDoesNotExist.

Here is the installed apps section of the settings.py file.

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

The file that I am trying to load is news.html located in the Templates folder of the news app.

Here is the TEMPLATES declaration in settings.py.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'moviereviews/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',
            ],
        },
    },
]

Here is the entry in the urls.py entry for the main project folder.

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', movieViews.home, name='home'),
    path('about/', movieViews.about, name='about'),
    path('signup/', movieViews.signup, name='signup'),
    path('news/', include('news.urls')),
]

The news.html template is in news/templates. If I move this html page to the templates folder in the main project application, it finds the page. However, if I remove it from that templates folder I get the TemplateDoesNotExist error.

I compared my code, line-by-line with the code that I pulled from Github for the book and don’t see any differences, but clearly I am missing something here.

What could be missing that is preventing the file news/templates/news.html from being found?

I’ve been searching for answers but with no success, so I’m hoping someone here can shed some light on this for me.

Thanks in advance!!

Please post the view where you’re trying to render this template.

Here is the code in views.py in the news app.

from django.shortcuts import render
from .models import News


# Create your views here.

def news(request):
    newss = News.objects.all().order_by('-date')
    return render(request, 'news.html', {'newss': newss})

Are you currently running with DEBUG=True or DEBUG=False?

If you’re running with DEBUG=True, the web page should return showing the directories being searched for that template. Please post that list here.

Hi Ken,

First and foremost, thanks for taking the time to respond to this. I really appreciate it.

I do have debugging set to True and I have I have attached a screen shot here.

Thanks again!

Bob

There should be more below that that shows the list of directories being searched for that template. That’s the information we’d be looking for.

Yes, you’re right. Sorry about that.

Here’s the full image including the directories being searched.

Now that I look at this, the last directory listed is telling. I have a “movie” app and it is trying to search that folder, but it does not search the “news” app folder. Somehow the “movie” app is registered properly but “news” is not.

Can you tell what I might be missing?

What are the contents of your “movie” directory? What are the contents of your “news” directory?

Here is the project folder…

Okay, you got me to resolve this one on my own!! Good job :grin:

I can see now that I inadvertently created the template folder in the wrong place. I just moved that into the News folder and now the links are working.

Thanks for all of your help on this one. I doubt I would have noticed this if you hadn’t asked for the contents of the folders.

2 Likes