Coverage for website/middleware.py: 71%

14 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2025-09-13 15:29 -0300

1from django.http import Http404 

2from django.shortcuts import render 

3 

4 

5class Custom404Middleware: 

6 def __init__(self, get_response): 

7 self.get_response = get_response 

8 

9 def __call__(self, request): 

10 response = self.get_response(request) 

11 

12 # If the response status code is 404, render our custom 404 page 

13 if response.status_code == 404: 

14 return render(request, "errors/404.html", status=404) 

15 

16 return response 

17 

18 def process_exception(self, request, exception): 

19 # Handle 404 exceptions 

20 if isinstance(exception, Http404): 

21 return render(request, "errors/404.html", status=404) 

22 

23 # For other exceptions, return None to let Django handle it 

24 return None