| 1 |
from django.conf import settings |
| 2 |
from django.contrib.flatpages.views import flatpage |
| 3 |
from django.http import Http404 |
| 4 |
from django.utils.deprecation import MiddlewareMixin |
| 5 |
|
| 6 |
|
| 7 |
class FlatpageFallbackMiddleware(MiddlewareMixin): |
| 8 |
def process_response(self, request, response): |
| 9 |
if response.status_code != 404: |
| 10 |
return response # No need to check for a flatpage for non-404 responses. |
| 11 |
try: |
| 12 |
return flatpage(request, request.path_info) |
| 13 |
# Return the original response if any errors happened. Because this |
| 14 |
# is a middleware, we can't assume the errors will be caught elsewhere. |
| 15 |
except Http404: |
| 16 |
return response |
| 17 |
except Exception: |
| 18 |
if settings.DEBUG: |
| 19 |
raise |
| 20 |
return response |