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

3 horizontal lines, burger
Remove all
LOADING ...

Content



    Custom 404, 500 error pages in Django

    Clock
    12.04.2025
    /
    Clock
    01.10.2025
    /
    Clock
    3 minutes
    An eye
    1187
    Hearts
    0
    Connected dots
    0
    Connected dots
    0
    Connected dots
    0

    Initial project setup

    Before you start adding custom pages for 404 and 500 responses, you will need to set up your project.
    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.
    We do this only because we need to see the result of our changes. And if we don't do this, we will constantly get pages like this, no matter what we do:
    And that's about it, but not quite. If, of course, your site is not yet styled, then you can safely skip this chapter to the end. But let's be honest, most likely you already have some styling on your site and that's why you're reading this article. Because you want to complete the layout/styling of the site, or more specifically, the layout/styling of 404, 500 and other responses from the server.
    And after completing all the steps described above, you expect to see something like this:
    All styles and scripts loaded and applied, it is just beauty ⊂(・ω・*⊂)
    But you'll 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 and others

    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 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.
    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.

    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


    What to do if pagination pages with canonical tags appear in the search index and such errors as Duplicate|Similar content appear?

    Clock
    02.03.2025
    /
    Clock
    02.10.2025
    An eye
    520
    Hearts
    0
    Connected dots
    0
    Connected dots
    0
    Connected dots
    0
    Why you should be careful while adding a canonical tag for a pagination page, or what to do if you now have a ton of duplicate pages in GSC plus …

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

    Clock
    01.04.2025
    /
    Clock
    01.10.2025
    An eye
    357
    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
    01.10.2025
    An eye
    753
    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
    01.10.2025
    An eye
    469
    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
    01.10.2025
    An eye
    1078
    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
    01.10.2025
    An eye
    963
    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