Django: Handling TemplateDoesNotExist Exceptions with Base HTML

Ann Jul 09, 2026

When working with Django, a popular web framework for Python, you might encounter the 'TemplateDoesNotExist' exception. This typically occurs when Django can't find the template you've specified in your view. Understanding how to handle this exception and use a base HTML template is crucial for a smooth development process.

Django custom 500 error template page
Django custom 500 error template page

In this guide, we'll delve into the 'TemplateDoesNotExist' exception, explain how to handle it, and demonstrate how to use a base HTML template in Django.

Chapter 12: Implementing Schema Markup
Chapter 12: Implementing Schema Markup

Understanding the 'TemplateDoesNotExist' Exception

Django follows a specific naming convention and directory structure for templates. When you specify a template in your view, Django looks for it in a specific directory structure. If it can't find the template, it raises the 'TemplateDoesNotExist' exception.

Understanding Context in Django Templates
Understanding Context in Django Templates

Here's a simple example. Suppose you have the following view:

from django.shortcuts import render

def my_view(request):
    return render(request, 'my_app/my_template.html')

If Django can't find 'my_template.html' in the 'my_app' directory, it will raise the 'TemplateDoesNotExist' exception.

the diagram shows how to use django
the diagram shows how to use django

Handling the 'TemplateDoesNotExist' Exception

To handle this exception, you can use Django's built-in exception handling middleware. Here's how you can do it:

from django.shortcuts import render
from django.http import Http404

def my_view(request):
    try:
        return render(request, 'my_app/my_template.html')
    except Http404:
        return render(request, 'my_app/error_template.html')

In this example, if 'my_template.html' doesn't exist, Django will render 'error_template.html' instead.

Django: MVT(Model-View-Template)
Django: MVT(Model-View-Template)

Using a Base HTML Template

Django allows you to create a base template that you can extend in other templates. This is useful for maintaining a consistent layout across your website. Here's how you can create and use a base HTML template:

First, create a base template, say 'base.html':

📊 Django Templates vs Django APIs: Key Differences
📊 Django Templates vs Django APIs: Key Differences

<!DOCTYPE html>
<html>
<head>
    <title>My Website</title>
</head>
<body>
    <div id="content">
        {% block content %}
        </div>
    </body>
</html>

The '{% block content %}' directive is where you'll insert content from other templates.

Now, create another template that extends 'base.html':

Template for List Page in Django
Template for List Page in Django
🔗 Django Models: OneToOneField (One-to-One Relationship)
🔗 Django Models: OneToOneField (One-to-One Relationship)
a diagram with the words django in different languages
a diagram with the words django in different languages
Django Insert Data Into Database with Source Code
Django Insert Data Into Database with Source Code
Customizing Views: Filter Lists in Django Admin
Customizing Views: Filter Lists in Django Admin
Understanding Django's MVT Framework
Understanding Django's MVT Framework
Django Project Ideas for Beginners to Experts
Django Project Ideas for Beginners to Experts
Django Bootstrap Base Template
Django Bootstrap Base Template
📄 Template with Model Relations in Django
📄 Template with Model Relations in Django
Why Django Framework is Best For Web Development?
Why Django Framework is Best For Web Development?
⚙️ Django Generic Views – Build Faster with Less Code
⚙️ Django Generic Views – Build Faster with Less Code
Secure Your Django Project with .env Files
Secure Your Django Project with .env Files
Add Links with the {% url %} Tag in Django Templates
Add Links with the {% url %} Tag in Django Templates
an image of a web page with the text structure in red and white on it
an image of a web page with the text structure in red and white on it
Create Views in Django
Create Views in Django
Fasto - Django Saas Admin Dashboard Template
Fasto - Django Saas Admin Dashboard Template
14 Django Website Template | Online Training Download app from below link
14 Django Website Template | Online Training Download app from below link
Django Models – choices Option
Django Models – choices Option
File Management System in Django with Source Code
File Management System in Django with Source Code
Learn most prominent advantages and disadvantages of Django
Learn most prominent advantages and disadvantages of Django

{% extends 'base.html' %}

{% block content %}
    <h1>Welcome to my website!</h1>
{% endblock %}

In this template, '{% extends 'base.html' %}' tells Django to use 'base.html' as the parent template. The content between '{% block content %}' and '{% endblock %}' replaces the 'content' block in 'base.html'.

Debugging Template Issues

When you encounter template-related issues, Django's debug mode can be a helpful tool. It provides detailed error messages that can guide you towards the solution.

To enable debug mode, set 'DEBUG = True' in your Django project's settings.py file. However, remember to set it to 'False' in production to prevent exposing sensitive information.

Using Django's Template Debug Toolbar

Django's template debug toolbar is another useful tool for debugging template issues. It provides information about the template loading process, template variables, and more. To use it, install it via pip:

pip install django-debug-toolbar

Then, add 'debug_toolbar' to your INSTALLED_APPS in settings.py and configure the middleware in the MIDDLEWARE setting.

In conclusion, understanding and handling the 'TemplateDoesNotExist' exception and using base HTML templates are essential skills for any Django developer. They help ensure your website remains functional and consistent, even in the face of template-related issues. Happy coding!