bag-of-holding.com

What's in the bag?

/2008/November/28

FlatPageOverride

Maybe, for some reason, you want to grant an administrator of your Django powered website the ability to override an existing page at will. Possibly, this override would be temporary, perhaps not.

Why would you want to do such a thing? Well, maybe this scenario occurs frequently enough, that you don't want to be bothered to modify the urlconf, necessitating a web server restart every time it happens. Wouldn't it be nice if they could do this without having to consult a developer or worry about breaking the site when they tried to muck with the source code?

We can easily implement a piece middleware to do this. The following is how I dealt with this exact scenario::

# flatpageoveride.middleware.py
from django.contrib.flatpages.views import flatpage
from django.http import Http404
from django.conf import settings

class FlatpageOverrideMiddleware(object):
    def process_request(self, request):
        try:
            return flatpage(request, request.path)
        except Http404:
            return None
        except:
            if settings.DEBUG:
                raise
            return response

To activate this middleware, simply add it the MIDDLEWARE_CLASSES in your settings.py file.

Comments

Add a comment