Обработка ошибки 404 django

The web framework for perfectionists with deadlines.

Writing views¶

A view function, or view for short, is a Python function that takes a
web request and returns a web response. This response can be the HTML contents
of a web page, or a redirect, or a 404 error, or an XML document, or an image .
. . or anything, really. The view itself contains whatever arbitrary logic is
necessary to return that response. This code can live anywhere you want, as long
as it’s on your Python path. There’s no other requirement–no “magic,” so to
speak. For the sake of putting the code somewhere, the convention is to
put views in a file called views.py, placed in your project or
application directory.

A simple view¶

Here’s a view that returns the current date and time, as an HTML document:

from django.http import HttpResponse
import datetime

def current_datetime(request):
    now = datetime.datetime.now()
    html = "<html><body>It is now %s.</body></html>" % now
    return HttpResponse(html)

Let’s step through this code one line at a time:

  • First, we import the class HttpResponse from the
    django.http module, along with Python’s datetime library.

  • Next, we define a function called current_datetime. This is the view
    function. Each view function takes an HttpRequest
    object as its first parameter, which is typically named request.

    Note that the name of the view function doesn’t matter; it doesn’t have to
    be named in a certain way in order for Django to recognize it. We’re
    calling it current_datetime here, because that name clearly indicates
    what it does.

  • The view returns an HttpResponse object that
    contains the generated response. Each view function is responsible for
    returning an HttpResponse object. (There are
    exceptions, but we’ll get to those later.)

Django’s Time Zone

Django includes a TIME_ZONE setting that defaults to
America/Chicago. This probably isn’t where you live, so you might want
to change it in your settings file.

Mapping URLs to views¶

So, to recap, this view function returns an HTML page that includes the current
date and time. To display this view at a particular URL, you’ll need to create a
URLconf; see URL dispatcher for instructions.

Returning errors¶

Django provides help for returning HTTP error codes. There are subclasses of
HttpResponse for a number of common HTTP status codes
other than 200 (which means “OK”). You can find the full list of available
subclasses in the request/response
documentation. Return an instance of one of those subclasses instead of a
normal HttpResponse in order to signify an error. For
example:

from django.http import HttpResponse, HttpResponseNotFound

def my_view(request):
    # ...
    if foo:
        return HttpResponseNotFound('<h1>Page not found</h1>')
    else:
        return HttpResponse('<h1>Page was found</h1>')

There isn’t a specialized subclass for every possible HTTP response code,
since many of them aren’t going to be that common. However, as documented in
the HttpResponse documentation, you can also pass the
HTTP status code into the constructor for HttpResponse
to create a return class for any status code you like. For example:

from django.http import HttpResponse

def my_view(request):
    # ...

    # Return a "created" (201) response code.
    return HttpResponse(status=201)

Because 404 errors are by far the most common HTTP error, there’s an easier way
to handle those errors.

The Http404 exception¶

class django.http.Http404

When you return an error such as HttpResponseNotFound,
you’re responsible for defining the HTML of the resulting error page:

return HttpResponseNotFound('<h1>Page not found</h1>')

For convenience, and because it’s a good idea to have a consistent 404 error page
across your site, Django provides an Http404 exception. If you raise
Http404 at any point in a view function, Django will catch it and return the
standard error page for your application, along with an HTTP error code 404.

Example usage:

from django.http import Http404
from django.shortcuts import render
from polls.models import Poll

def detail(request, poll_id):
    try:
        p = Poll.objects.get(pk=poll_id)
    except Poll.DoesNotExist:
        raise Http404("Poll does not exist")
    return render(request, 'polls/detail.html', {'poll': p})

In order to show customized HTML when Django returns a 404, you can create an
HTML template named 404.html and place it in the top level of your
template tree. This template will then be served when DEBUG is set
to False.

When DEBUG is True, you can provide a message to Http404 and
it will appear in the standard 404 debug template. Use these messages for
debugging purposes; they generally aren’t suitable for use in a production 404
template.

Customizing error views¶

The default error views in Django should suffice for most web applications,
but can easily be overridden if you need any custom behavior. Specify the
handlers as seen below in your URLconf (setting them anywhere else will have no
effect).

The page_not_found() view is overridden by
handler404:

handler404 = 'mysite.views.my_custom_page_not_found_view'

The server_error() view is overridden by
handler500:

handler500 = 'mysite.views.my_custom_error_view'

The permission_denied() view is overridden by
handler403:

handler403 = 'mysite.views.my_custom_permission_denied_view'

The bad_request() view is overridden by
handler400:

handler400 = 'mysite.views.my_custom_bad_request_view'

Testing custom error views¶

To test the response of a custom error handler, raise the appropriate exception
in a test view. For example:

from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
from django.test import SimpleTestCase, override_settings
from django.urls import path


def response_error_handler(request, exception=None):
    return HttpResponse('Error handler content', status=403)


def permission_denied_view(request):
    raise PermissionDenied


urlpatterns = [
    path('403/', permission_denied_view),
]

handler403 = response_error_handler


# ROOT_URLCONF must specify the module that contains handler403 = ...
@override_settings(ROOT_URLCONF=__name__)
class CustomErrorHandlerTests(SimpleTestCase):

    def test_handler_renders_template_response(self):
        response = self.client.get('/403/')
        # Make assertions on the response here. For example:
        self.assertContains(response, 'Error handler content', status_code=403)

Async views¶

As well as being synchronous functions, views can also be asynchronous
(“async”) functions, normally defined using Python’s async def syntax.
Django will automatically detect these and run them in an async context.
However, you will need to use an async server based on ASGI to get their
performance benefits.

Here’s an example of an async view:

import datetime
from django.http import HttpResponse

async def current_datetime(request):
    now = datetime.datetime.now()
    html = '<html><body>It is now %s.</body></html>' % now
    return HttpResponse(html)

You can read more about Django’s async support, and how to best use async
views, in Asynchronous support.

Python Django: как изменить страницу ошибки 404?

Настройка страницы ошибки 404 “Страница не найдена”  —  одна из самых любопытных практик в разработке проекта на Django: сейчас же давайте узнаем, как обработать ошибку 404 с помощью собственной страницы!

Skillfactory.ru

404 Page Not Found считается самым известным кодом HTTP-статуса. На стадии коммерческого использования каждый хороший Django-проект должен обрабатывать эту ошибку с помощью собственноручно настроенной страницы, создающейся за несколько простых шагов, описанных в данной статье.

Не теряя времени, создадим Django-проект. Откройте консоль и напишите эти команды:

$ cd Desktop
$ mkdir mysite
$ cd mysite

Затем создайте Django-проект:

$ django-admin startproject mysite .

Запустите сервер со своим веб-приложением:

$ python manage.py runserver

В браузере перейдите по ссылке http://127.0.0.1:8000/ (локальный сервер, порт номер 8000): если вы увидите страницу с надписью “The install worked successfully! Congratulation!” (“Установка выполнена успешно! Поздравляем!”) и картинкой ракеты, то проект успешно создан и запущен.

Стандартная страница приветствия фреймворка Django

Давайте попробуем перейти на несуществующую страницу только что созданного нами сайта, например, на страницу http://127.0.0.1:8000/page

Страница Django Not Found по умолчанию

Сообщение внизу страницы гласит:

You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

Вы видите эту ошибку, потому что в вашем файле настроек Django установлено значение DEBUG = True. Измените это значение на False, и Django отобразит стандартную страницу 404.

Давайте сделаем то, что написано в отчёте об ошибке 404: отключим режим отладки Django, изменив значение константы DEBUGв файле настроек проекта settings.py, кроме этого, указываем адрес локального сервера в списке разрешенных адресов:

DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1']

Замечание! Когда вы будете располагать своё веб-приложение на хостинге для доступа к нему настоящих пользователей, вам понадобится снова изменить константу ALLOWED_HOSTS: указать приобретенный домен в качестве элемента списка.

Обновите страницу.

Страница Django Not Found 404 при значении константы DEBUG = False (при отключенном режиме разработчика)

Это и есть та самая страница, которую мы сейчас настроим с помощью собственного пользовательского шаблона.

Создаём файл представлений views.py в папке mysite с помощью данной команды:

$ touch mysite/views.py

Действительно, немного странно  —  создавать файл views.py в каталоге проекта. Во множестве руководств говорится, что views.py хранится в директориях каждого приложения в проекте, а не в директории самого проекта, но именно такой способ создания пользовательской страницы ошибки 404 “Страница не найдена” предлагается официальной документацией Django.

Ваш Django-проект теперь должен выглядеть примерно так:

.
├── db.sqlite3
├── manage.py
└── mysite
    ├── __init__.py
    ├── __pycache__
    ├── asgi.py
    ├── settings.py
    ├── urls.py
    ├── views.py
    └── wsgi.py

Теперь пришло время добавить функцию-представление, которая будет обрабатывать ошибку 404. В файл mysite/views.py добавьте следующие строчки кода:

from django.shortcuts import render

def page_not_found_view(request, exception):
    return render(request, '404.html', status=404)

Также необходимо в файлеurls.py директории Django-проекта mysite определить значение переменной handler404:

handler404 = "mysite.views.page_not_found_view"

В итоге файл mysite/urls.py должен выглядеть следующим образом:

from django.contrib import admin
from django.urls import path

urlpatterns = [
    path('admin/', admin.site.urls),
]

handler404 = "django_404_project.views.page_not_found_view"

Теперь пришло время поработать с шаблоном страницы ошибки 404 “Страница не найдена”. Для этого нужно создать папку templates в директории проекта mysite, а затем добавить в эту папку файл 404.html.

$ mkdir templates
$ touch templates/404.html

Теперь необходимо указать Django, где именно находится ваша папка templates, поэтому в файле settings.py, в константе TEMPLATES следует обозначить значение ключа ‘DIRS’ следующим образом:

import os

TEMPLATES = [
    {
        ...
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        ...
    },
]

Теперь, в файле шаблона 404.html, можно начинать настраивать внешний вид страницы сообщения об ошибке по своему усмотрению. Для примера сделаем её крайне простой:

<h1>404 Page Not Found</h1>
<p>Моя пользовательская страница ошибки 404!</p>

Перед тем, как переключиться на вкладку браузера для проверки только что созданного шаблона  —  убедитесь, что структура вашего проекта выглядит вот так:

.
├── db.sqlite3
├── manage.py
├── mysite
│   ├── __init__.py
│   ├── __pycache__
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   ├── views.py
│   └── wsgi.py
└── templates
    └── 404.html

После проверки структуры проекта — перейдите по любому из несуществующих путей вашего сайта: пользовательский HTML-шаблон 404.html обработает их все!

Skillfactory.ru

Пользовательская страница ошибки “404 Page Not Found”

Поздравляем  —  всё готово! Ваша собственноручно созданная страница 404 Page Not Found теперь используется каждый раз, когда пользователь обращается к несуществующему ресурсу.

Читайте также:

  • 3 способа локального хранения и чтения учетных данных в Python
  • Что нового в Python 3.10?
  • Python и веб-разработка: краткое руководство

Читайте нас в Telegram, VK и Яндекс.Дзен


Перевод статьи Görkem Arslan: Django: Customize 404 Error Page

According to the documentation this should be fairly simple: I just need to define handler404. Currently I am doing, in my top urls.py:

urlpatterns = [
    ...
]

handler404 = 'myapp.views.handle_page_not_found'

The application is installed. The corresponding view is just (for the time being I just want to redirect to the homepage in case of 404):

def handle_page_not_found(request):
    return redirect('homepage')

But this has no effect: the standard (debug) 404 page is shown.

The documentation is a bit ambiguous:

  • where should handler404 be defined? The documentation says in the URLconf but, where exactly? I have several applications, each with a different urls.py. Can I put it in any of them? In the top URLconf? Why? Where is this documented?
  • what will be catched by this handler? Will it catch django.http.Http404, django.http.HttpResponseNotFound, django.http.HttpResponse (with status=404)?

asked Feb 2, 2016 at 14:29

blueFast's user avatar

6

As we discussed, your setup is correct, but in settings.py you should make DEBUG=False. It’s more of a production feature and won’t work in development environment(unless you have DEBUG=False in dev machine of course).

answered Feb 2, 2016 at 14:44

Shang Wang's user avatar

Shang WangShang Wang

24.4k19 gold badges68 silver badges91 bronze badges

2

All the other answers were not up to date. Here’s what worked for me in Django 3.1:

urls.py

from django.conf.urls import handler404, handler500, handler403, handler400
from your_app_name import views

handler404 = views.error_404
handler500 = views.error_500

views.py

def error_404(request, exception):
   context = {}
   return render(request,'admin/404.html', context)

def error_500(request):
   context = {}
   return render(request,'admin/500.html', context)

Note, you will have to edit this code to reflect your app name in the import statement in urls.py and the path to your html templates in views.py.

answered Oct 25, 2020 at 19:40

devdrc's user avatar

devdrcdevdrc

1,68614 silver badges21 bronze badges

5

Debug should be False and add to view *args and **kwargs. Add to urls.py handler404 = 'view_404'

def view_404(request, *args, **kwargs):
return redirect('https://your-site/404')

If I didn’t add args and kwargs server get 500.

answered Nov 22, 2018 at 9:45

Pasha M's user avatar

Pasha MPasha M

2113 silver badges5 bronze badges

To render 404 Error responses on a custom page, do the following:

In your project directory open settings.py and modify DEBUG as follows:

    DEBUG = False

In the same directory create a file and name it views.py, insert the following code:

   from django.shortcuts import render

   def handler404(request, exception):
       return render(request, 'shop/shop.html')

Finally open urls.py file which is in the same project directory and add the following code:

   from django.contrib import admin
   from . import views

   handler404 = views.handler404

   urlpatterns = [
      path('admin/', admin.site.urls),
   ]

answered Oct 16, 2019 at 10:45

GilbertS's user avatar

GilbertSGilbertS

5638 silver badges11 bronze badges

  1. 1. Кастомизация ошибок 404, 500
  2. 2. Кастомизация ошибки 403

Многие ресурсы имеют оформленные страницы ошибок, если происходит сбой в обработке запроса от клиента.

Для начала на сайте была сделана кастомизация наиболее часто возникающих ошибок, другие при отладке пока не попадались, но всё впереди.

Как объявлено в заголовке статьи, кастомизированы был следующие ошибки:

  1. 403 — Ошибка авторизации, доступ запрещён.
  2. 404 — Страница не найдена;
  3. 500 — Внутренняя ошибка сервера;

Кастомизация ошибок 404, 500

Для кастомизации ошибок 404 и 500 необходимо написать обработчики запросов, и достаточно написать их представления в виде метода.

В шаблоны добавляем свои кастомизированные html файлы, то есть:

  • error404.html
  • error500.html

Модуль, в котором реализованы представления для данного сайта — это

home.

В шаблонах этого же модуля помещены сами шаблоны кастомизированных ошибок.

В файле

urls.py

главного модуля сайта переопределяем обработчики по умолчанию:

  • handler404
  • handler500

В коде это выглядит так:

from home.views import e_handler404, e_handler500

handler404 = e_handler404
handler500 = e_handler500

Опишем представления в файле

views.py

модуля

home:

from django.shortcuts import render_to_response
from django.template import RequestContext


def e_handler404(request):
    context = RequestContext(request)
    response = render_to_response('error404.html', context)
    response.status_code = 404
    return response


def e_handler500(request):
    context = RequestContext(request)
    response = render_to_response('error500.html', context)
    response.status_code = 500
    return response

Кастомизация ошибки 403

Ошибка 403 возникает в том случае, когда не авторизованный пользователь пытается получить доступ к той части сайта, в которую доступ разрешён только авторизованным пользователям.

В Django это достигается за счёт проверки статуса пользователя и добавления на страницы защитного токена, механизм

CSRF.

Данная ошибка может возникнуть и в том случае, если пользователь авторизован, но совершает действия, при которых требуется проверка токена CSRF, а сам токен был потерян или не верен. Дело в том, что для корректности работы токена, необходимо добавлять в шаблоне в формах специальный тег:

{% csrf_token %}

В него и будет подставляться токен, но просто добавить в шаблон, его не достаточно. Прежде, чем начать рендер шаблон, необходимо добавить токен в контекст, который будет передан в шаблон. То есть,

from django.template.context_processors import csrf
from django.shortcuts import render_to_response


def any_request(request):
    context = {}
    context.update(csrf(request))

    ...
    return render_to_response('any_request.html', context=context)

Ну а теперь ближе к непосредственно кастомизации. Для работы csrf необходимо, чтобы в файле

settings.py

добавлен модуль csrf и указано представление, которое будет заниматься обработкой данной ошибки:

MIDDLEWARE = [
    ...
    'django.middleware.csrf.CsrfViewMiddleware',
    ...
]

CSRF_FAILURE_VIEW = 'home.views.csrf_failure'

В шаблонах добавим

error403.html,

а в файле

views.py

пропишем обработчик представления.

def csrf_failure(request, reason=""):
    context = RequestContext(request)
    response = render_to_response('error403.html', context)
    response.status_code = 403
    return response

Для

Django

рекомендую

VDS-сервера хостера Timeweb

.

Понравилась статья? Поделить с друзьями:
  • Обработка запроса провалилась ошибка на этапе обработки документа системой
  • Обоснование исправления реестровой ошибки границ земельного участка
  • Оборудование для сброса ошибок на автомобиле
  • Оборудование для проверки ошибок на автомобиле
  • Оболочка вне зоны ошибка сварка оптики