This snippet is a piece of middleware that can be used to allow an administrator to create a flatpage that will override an existing URL.
Why would you want such a thing? Maybe an admin needs to be able to override individual detail pages of an item list at will and doesn't want to be bothered mucking around with urlconf and restarting apache2.
Obviously, it's not the ideal situation for permanent overrides (in that case, you'd be better off writing an exception in the urlconf), but it's good for the temporary, one-off, situations that may arise for whatever reasons.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Please login first before commenting.