[Django] Create your own 403, 404, 500 error pages

Overview

I've investigated how to create an error page such as 404 Not Found, so I'll summarize it.

The error I checked this time

I will touch on the following error codes.

manner

Under any templates directory,

Place. Then, when the target error is thrown, the template will be read automatically.

Example

myproject
  - myapp
    - urls.py
    - views.py
    - models.py
    - app.py
    - forms.py
    - templates
      - 403.html
      - 404.html
      - 500.html

Implement your own 404 page etc. as below.

404.html


{% extends "base.html" %}
{% block content %}
<section>
    <div>
        <label>404 Not Found<br>The page you are looking for could not be found.<br><a class="hover-under" href="{% url 'top' %}">Back to top page</a></label>
    </div>
</section>
{% endblock %}

Exception that each error code is thrown out

These pages will be returned if you call an exception for which each status code should be returned.

404 Not Found Raise the Http404 class inside the django.http package.

views.py


from django.http import Http404


class MyView(View):
    def index(self):
        raise Http404

403 Forbidden There is a PermissionDenied class in django.core.exceptions.

views.py


from django.core.exceptions import PermissionDenied


class MyView(View):
    def index(self):
        if not self.request.user.is_authenticated:
            raise PermissionDenied

500 Internal Server Error Use a normal error that doesn't catch or explicitly returns something that would result in a normal InternalServerError, such as a syntax error.

views.py


from .models import Example


class MyView(View):
    def index(self):
        try:
            _instance = Example.objects.get(id=1)
        except Example.DoesNotExists:
            #Throw an exception (usually don't do much)
            raise Exception

Recommended Posts

[Django] Create your own 403, 404, 500 error pages
Create your own Django middleware
Create your own exception
How to create your own Transform
Create your own name resolution service
Your own Twitter client made with Django
Create your own Linux commands in Python
Create your own DNS server with Twisted
Create a model for your Django schedule
Create your own Composite Value with SQLAlchemy
Create your first app with Django startproject
Create a wheel of your own OpenCV module
Django2.2 SQLite3 version error
Create a Django schedule
Memo to create your own Box with Pepper's Python
Create your own Big Data in Python for validation
Django settings.py SECRET_KEY error
Create your own Random Dot Stereogram (RDS) in Python.
[Blender × Python] Create your own function & summary so far
Create Django Todo list
Reinforcement learning 23 Create and use your own module with Colaboratory
Create an API with Django
Django beginners create simple apps 3
Django beginners create simple apps 1
Tuning your Django admin site
Create ToDo List [Python Django]
Create a homepage with django
Django beginners create simple apps 2
Create a Django login screen
Install Django on your Mac
Django Server Error (500) Strategy [2019 Adcare]
Create and list Django models
Django beginners create simple apps 5
Try docker: Create your own container image for your Python web app
Create your own IoT platform using raspberry pi and ESP32 (Part 1)