https://docs.djangoproject.com/en/3.0/topics/http/views/#customizing-error-views
# urls.py
from Blog.views import HomeView, ErrorView, ErrorView2
...
# 방법1
handler400 = ErrorView.as_view()
handler403 = ErrorView.as_view()
handler404 = ErrorView.as_view()
handler500 = ErrorView.as_view()
# 방법2
handler400 = ErrorView2.viewPage
handler403 = ErrorView2.viewPage
handler404 = ErrorView2.viewPage
handler500 = ErrorView2.viewPage
# views.py
from django.views.generic.base import TemplateView
from django.http import request
from django.http.response import HttpResponse
from django.shortcuts import render_to_response, render
from django.template import loader
from django.template.context import RequestContext
class ErrorView(TemplateView):
template_name = "error/error.html"
def get_context_data(self, **kwargs):
context = TemplateView.get_context_data(self, **kwargs)
context["error_code"] = 404
return context
class ErrorView2():
@classmethod
def viewPage(cls, request, exception=None):
#return HttpResponse("Error!!", status=404)
#=======================================================================
# response = HttpResponse()
# response.write("hello!</br>")
# response.write("page not found")
# response["error_code"] = 404
# return response
#=======================================================================
#t = loader.get_template("error/error.html")
#return HttpResponse(t.render({"error_code":"404"}, request))
return render(request, "error/error.html", {"error_code":"404", "exception":exception}, status=404)
# deprecated
#return render_to_response("error/error.html", {"error_code":"404"}, RequestContext(request), status=404)
#return render_to_response("error/error.html", {"error_code":"404"}, RequestContext(request))
#return render_to_response('error/error.html', {"error_code":"404"})
# /templates/error/error.html
{% extends "default.html" %}
{% block title %}Raise error{% endblock title %}
{% block content %}
{{ error_cdoe }}
{% endblock content %}