2015-02-10 02:19:34 +08:00
|
|
|
from django.conf import settings
|
2005-11-11 12:45:05 +08:00
|
|
|
from django.contrib.flatpages.views import flatpage
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.http import Http404
|
2005-11-11 12:45:05 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2006-06-08 13:00:13 +08:00
|
|
|
class FlatpageFallbackMiddleware(object):
|
2005-11-11 12:45:05 +08:00
|
|
|
def process_response(self, request, response):
|
|
|
|
if response.status_code != 404:
|
2013-11-03 05:02:56 +08:00
|
|
|
return response # No need to check for a flatpage for non-404 responses.
|
2005-11-11 12:45:05 +08:00
|
|
|
try:
|
2008-08-21 09:40:22 +08:00
|
|
|
return flatpage(request, request.path_info)
|
2005-11-11 12:45:05 +08:00
|
|
|
# Return the original response if any errors happened. Because this
|
|
|
|
# is a middleware, we can't assume the errors will be caught elsewhere.
|
2005-11-20 12:55:57 +08:00
|
|
|
except Http404:
|
|
|
|
return response
|
2013-09-30 23:55:14 +08:00
|
|
|
except Exception:
|
2006-05-02 09:31:56 +08:00
|
|
|
if settings.DEBUG:
|
2005-11-11 12:45:05 +08:00
|
|
|
raise
|
|
|
|
return response
|