3 horizontal lines, burger
3 horizontal lines, burger
3 horizontal lines, burger
3 horizontal lines, burger

3 horizontal lines, burger
Remove all
LOADING ...

Content



    How to Customize 404 Response Pages in Django, Two Ways

    Clock
    12.04.2025
    /
    Clock
    30.03.2026
    /
    Clock
    4 minutes
    An eye
    2286
    Hearts
    0
    Connected dots
    0
    Connected dots
    0
    Connected dots
    0

    Introduction

    Why bother customizing and designing error pages like 404? It's mostly to make your site look and feel consistent. As a bonus, if a user lands on such a page, you can suggest other similar pages, thereby avoiding bounces and increasing page depth and viewing time. (There for the second method is preferable)
    As a developer, you might not understand what bounce rate, depth, and time have to do with it. In short, these are key visitor behavioral factors that can impact a website's search engine rankings.
    In this article, I'll show two ways to customize these pages for Django projects:
    1. Via overriding default templates
    2. Via defining your own views
    This article will focus on 404 errors, but everything described here also applies to other error types, such as 500 (Server Error), 403 (Forbidden), and 400 (Bad Request).
    You can read a more detailed description of these errors and their meaning in the links provided above, and we'll continue.

    Preparing for debugging and running on a local server

    Why to do this anyway?

    Before adding and designing a 404 page, you need to prepare the project to display it. Why? You ask. The thing is, in debug mode (Debug=False), we'll always get a page like this:
    And if we put the server into the deployed state, that is, Debug=False, we will receive a standard page from the server:
    Now that we've figured out why you need to prepare your server to handle 404 errors, we can move on to the actual configuration and preparation.

    Preparing Django to work with error pages like 404

    First, create and add a directory with templates to the list of directories, the one in settings.py. And if your directories with templates are called templates/, then you don't have to change anything. But if you call them differently, do the following:
    TEMPLATES = [{ 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [ os.path.join(BASE_DIR, 'custom-templates-directory/'), ], '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', ], }, },]
    Add a new path to DIRS. You can find this one in the settings.py file.
    Secondly, put the site into production mode, that is, disable all debugging processes that Django runs with Debug=True in settings.py. You will also need to add the host name to ALLOWED_HOSTS, as follows:
    DEBUG = False ALLOWED_HOSTS = ['*']
    I am using a special character that kind of says, hey, this server is accessible from any address, enjoy! Don't forget to remove this special character after debugging or after deploying to a live server.
    Don't forget to remove this special character after debugging or after deploying to a live server.
    And that's about it, but not quite.

    Activating styles during the debugging process

    If, of course, your site isn't styled yet, you can safely skip this chapter. But let's be honest, you probably already have some styling on your site, and that's why you're reading this article.
    And after completing all the steps described above, you expect to see something like this:
    The client failed to get static files from the server. This is confirmed by the developer console.
    So, how do we get styles, scripts, and media from the server?
    To fix this, we need to slightly change the command to start the development server. Namely, start the server without any protective mechanisms, that is, instead of the command:
    python manage.py runserver
    You must use, this instead:
    python manage.py runserver --insecure
    Now, all static and media files should be available during the production server startup. You remember that we do this in order to see our own 404 and 500 response pages?

    Custom 404 page

    The first way is by overriding the default template.

    There are several ways to customize the 404 page. The first, and easiest, is to make a 404.html template in any templates/ directory in any application.
    If you want Django to use a different name, change the value of the global variable in settings.py. From ERROR_404_TEMPLATE_NAME="404.html" to ERROR_404_TEMPLATE_NAME="EXAMPLE_404.html".
    The same applies to server responses such as 500, 403, and 400.

    The second way is by defining your own views

    The second method offers more customization than the first. So, if you need to further customize pages with server responses 400, 403, 404, and 500, you can write your own view and add whatever you want there. Let's write the most basic view for the 404 response, in a file at Backend/views.py:
    def page_not_found(request, exception): return render(request, '404.html', status=404)
    All that's left is to connect it to a special handler. In urls.py, in any application, in my case it's always the Backend application, add this line:
    handler404 = "Backend.views.page_not_found"
    There are also handlers for 500, 403 and 400 server responses, they correspond to handler500, handler403 and handler400 respectively.
    I would like to note that for 4xx codes views must accept 2 arguments - a request and an exception. And for 5xx only a request.

    Instead of a conclusion

    Here are two ways I know of how to make your own server response pages, like 404 or 500. The first one is, of course, preferable because it is simpler, which means you can make fewer mistakes. The second one is more customizable. But choice is yours.
    And also, when you are done with debugging, return everything to its original state. That is, DEBUG=True and clear the ALLOWED_HOSTS list.
    I hope this article was useful to you and now you are able to make your own 404 page.

    Do not forget to share, like and leave a comment :)

    Comments

    (0)

    captcha
    Send
    LOADING ...
    It's empty now. Be the first (o゚v゚)ノ

    Other

    Similar articles


    A lot of pages were indexed by Google. What to do, and should you?

    Clock
    02.03.2025
    /
    Clock
    23.03.2026
    An eye
    784
    Hearts
    0
    Connected dots
    0
    Connected dots
    0
    Connected dots
    0
    Thousands of pagination and filtering pages have been indexed. What can be done about this, and is it worth it? Should they be blocked from the index or perhaps redirected? …

    How to implement More button (loading more posts for pagination). Using Django, REST API, HTMx

    Clock
    01.04.2025
    /
    Clock
    24.03.2026
    An eye
    586
    Hearts
    0
    Connected dots
    0
    Connected dots
    0
    Connected dots
    0
    In this article I will describe a way how you can implement asynchronous loading of content (articles) using the "More" button, which is made using Django, REST API, HTMx and …

    How to implement pagination in Django + HTMx pr. 1

    Clock
    02.04.2025
    /
    Clock
    24.03.2026
    An eye
    926
    Hearts
    0
    Connected dots
    0
    Connected dots
    0
    Connected dots
    0
    In this article, I will describe how to create a paginator using Django and the HTMx library. And why it was so easy compared to the paginator on my site. …

    How to make simple paginator in Django and HTMx. Adding fitering and sorting feature. pr. 2

    Clock
    08.04.2025
    /
    Clock
    24.03.2026
    An eye
    603
    Hearts
    0
    Connected dots
    0
    Connected dots
    0
    Connected dots
    0
    In this article I will describe the process and main code blocks to add sorting and filtering feature to a paginator. This paginator is written in Django using HTMx.

    How to implement the feedback form in Django and HTMx

    Clock
    11.04.2025
    /
    Clock
    24.03.2026
    An eye
    1317
    Hearts
    0
    Connected dots
    0
    Connected dots
    0
    Connected dots
    0
    In this article, I will describe the way to add on your Django website feedback form using only HTMx and a little bit of DaisyUI as a UI library. Everything …

    Tutorial about how to add Django sitemap to website

    Clock
    17.04.2025
    /
    Clock
    11.03.2026
    An eye
    1363
    Hearts
    0
    Connected dots
    0
    Connected dots
    0
    Connected dots
    0
    In this article, I will describe the simplest and most understandable way to add a sitemap to a Django site via generator. Here you will find three different types of …

    Used termins


    Related questions