Skip to content

Instantly share code, notes, and snippets.

@dryan
Created August 29, 2024 20:04
Show Gist options
  • Save dryan/fd8fee493471b88e8bcaeabfbb89af90 to your computer and use it in GitHub Desktop.
Save dryan/fd8fee493471b88e8bcaeabfbb89af90 to your computer and use it in GitHub Desktop.
from django.views.defaults import page_not_found as django_page_not_found
from django.views.decorators.csrf import requires_csrf_token
from django.views.defaults import ERROR_404_TEMPLATE_NAME
@requires_csrf_token
def page_not_found(request, exception, template_name=ERROR_404_TEMPLATE_NAME):
path = request.path.strip("/")
# custom logic goes here
# see if there's a lowercase match
if request.path != request.path.lower():
try:
view, args, kwargs = resolve(request.path.lower())
view(*args, **kwargs)
return http.HttpResponseRedirect(request.path.lower())
except Exception:
# do nothing so we return the original exception
pass
return django_page_not_found(request, exception, template_name=template_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment