From d8ab85f46f94d8ea27487303797637ebc955ce68 Mon Sep 17 00:00:00 2001 From: jordan Date: Mon, 1 Aug 2011 10:21:46 +0000 Subject: [PATCH 1/1] adding middleware that handles custom pages git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@1141 0cfe37f9-358a-4d5e-be75-b63607b5c754 --- forum/middleware/custom_pages.py | 20 ++++++++++++++++++++ settings.py | 1 + 2 files changed, 21 insertions(+) create mode 100644 forum/middleware/custom_pages.py diff --git a/forum/middleware/custom_pages.py b/forum/middleware/custom_pages.py new file mode 100644 index 0000000..122793b --- /dev/null +++ b/forum/middleware/custom_pages.py @@ -0,0 +1,20 @@ +from django.contrib.flatpages.views import flatpage +from django.http import Http404 +from django.conf import settings + +from forum.views.meta import page + +class CustomPagesFallbackMiddleware(object): + def process_response(self, request, response): + if response.status_code != 404: + return response # No need to check for a flatpage for non-404 responses. + try: + return page(request) + # Return the original response if any errors happened. Because this + # is a middleware, we can't assume the errors will be caught elsewhere. + except Http404: + return response + except: + if settings.DEBUG: + raise + return response diff --git a/settings.py b/settings.py index 041f6ae..f42268b 100644 --- a/settings.py +++ b/settings.py @@ -25,6 +25,7 @@ MIDDLEWARE_CLASSES = [ 'forum.middleware.request_utils.RequestUtils', 'forum.middleware.cancel.CancelActionMiddleware', 'forum.middleware.admin_messages.AdminMessagesMiddleware', + 'forum.middleware.custom_pages.CustomPagesFallbackMiddleware', 'django.middleware.transaction.TransactionMiddleware', 'forum.middleware.django_cookies.CookiePostHandlerMiddleware', ] -- 2.39.5