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
- Mask sensitive data from logger by agusmakmun 1 week ago
- Template tag - list punctuation for a list of items by shapiromatron 1 year, 2 months ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 1 year, 2 months ago
- Serializer factory with Django Rest Framework by julio 1 year, 9 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 10 months ago
Comments
Please login first before commenting.