Plain is headed towards 1.0! Subscribe for development updates →

 1from plain.http import ResponseBase
 2from plain.templates import TemplateFileMissing
 3
 4from .templates import TemplateView
 5
 6
 7class ErrorView(TemplateView):
 8    status_code: int
 9
10    def __init__(self, *, status_code=None, exception=None) -> None:
11        # Allow creating an ErrorView with a status code
12        # e.g. ErrorView.as_view(status_code=404)
13        self.status_code = status_code or self.status_code
14
15        # Allow creating an ErrorView with an exception
16        self.exception = exception
17
18    def get_template_context(self):
19        context = super().get_template_context()
20        context["status_code"] = self.status_code
21        context["exception"] = self.exception
22        return context
23
24    def get_template_names(self) -> list[str]:
25        return [f"{self.status_code}.html", "error.html"]
26
27    def get_request_handler(self):
28        return self.get  # All methods (post, patch, etc.) will use the get()
29
30    def get_response(self) -> ResponseBase:
31        response = super().get_response()
32        # Set the status code we want
33        response.status_code = self.status_code
34        return response
35
36    def get(self):
37        try:
38            return super().get()
39        except TemplateFileMissing:
40            return self.status_code