1from__future__importannotations 2 3fromcollections.abcimportCallable 4fromtypingimportAny 5 6fromplain.httpimportResponseBase 7fromplain.templatesimportTemplateFileMissing 8 9from.templatesimportTemplateView101112classErrorView(TemplateView):13status_code:int1415def__init__(16self,*,status_code:int|None=None,exception:Any|None=None17)->None:18# Allow creating an ErrorView with a status code19# e.g. ErrorView.as_view(status_code=404)20self.status_code=status_codeorself.status_code2122# Allow creating an ErrorView with an exception23self.exception=exception2425defget_template_context(self)->dict:26context=super().get_template_context()27context["status_code"]=self.status_code28context["exception"]=self.exception29returncontext3031defget_template_names(self)->list[str]:32# Try specific status code template (e.g. "404.html")33return[f"{self.status_code}.html"]3435defget_request_handler(self)->Callable[[],Any]:36returnself.get# All methods (post, patch, etc.) will use the get()3738defget_response(self)->ResponseBase:39response=super().get_response()40# Set the status code we want41response.status_code=self.status_code42returnresponse4344defget(self)->ResponseBase|int:45try:46returnsuper().get()47exceptTemplateFileMissing:48returnself.status_code