Django is a powerful web framework that makes it easy to build complex web applications. However, even the best applications can sometimes encounter errors. When this happens, Django provides a default error page that is displayed to the user.

However, you may want to customize this error page to better match your application’s design and branding. In this tutorial, we’ll show you how to use a custom 500 error template in Django.

1. Create the Custom Error Template

The first step is to create the custom error template. In this example, we’ll create a template called 500.html.

Create a new file called 500.html in your project’s templates directory. If the directory doesn’t exist, create it.

<!-- 500.html -->
<html>
    <body>
        <h1>Internal server Error 500</h1>
    </body>
</html>

In the 500.html file, you can include any HTML or CSS you like to customize the appearance of the error page. For example, you might include a custom header, footer, or background image.

2. Update the Django Settings

Next, you need to update your Django settings to tell Django to use your custom error template.

Open your project’s settings.py file and add the following code:

TEMPLATES = [
    {
        ...
        'OPTIONS': {
            'debug': DEBUG,
            'context_processors': [
                ...
            ],
            'builtins': [
                ...
            ],
            'libraries': {
                ...
            },
            'string_if_invalid': '',
            'exception_handler': 'django.views.debug.server_error',
            'template_name': '500.html',  # <-- Add this line
        },
    },
]

This tells Django to use the 500.html template for 500 server errors.

3. Test the Custom Error Template

Finally, you should test your custom error template to make sure it’s working correctly.

To do this, you can raise an exception in your code to trigger a 500 server error. For example, you could add the following line to one of your views:

raise Exception('This is a test error')

When you visit the page that triggers the error, you should see your custom error template instead of the default Django error page.

def my_view(request):
    # Some code that might cause an error
    if some_condition:
        raise Exception('This is a test error')
    else:
        # Some code that runs if there's no error
        pass

In this example, my_view is a Django view function that might encounter an error under certain conditions. If some_condition is true, the view raises an exception with the message “This is a test error”. This will cause Django to display the custom 500 error template instead of the default error page. If some_condition is false, the view continues to execute normally without raising an exception.

Conclusion

In this tutorial, we’ve shown you how to use a custom 500 error template in Django. By following these steps, you can easily customize the appearance of your error pages to better match your application’s design and branding.

Output