1
0
Fork 0
mirror of https://github.com/Findus23/acronomy.git synced 2024-09-18 14:33:43 +02:00

don't use trailing slashes in URLs

This commit is contained in:
Lukas Winkler 2020-06-15 22:37:20 +02:00
parent af6827f91f
commit cfcbad2ee3
Signed by: lukas
GPG key ID: 54DE4D798D244853
3 changed files with 23 additions and 1 deletions

View file

@ -48,7 +48,8 @@ MIDDLEWARE = [
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'simple_history.middleware.HistoryRequestMiddleware'
'simple_history.middleware.HistoryRequestMiddleware',
'common.redirect.NoTrailingSlashMiddleware'
]
ROOT_URLCONF = 'acronomy.urls'

0
common/__init__.py Normal file
View file

21
common/redirect.py Normal file
View file

@ -0,0 +1,21 @@
from django.http import HttpResponseRedirect
class NoTrailingSlashMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
old_url: str = request.path
if (
old_url.endswith('/')
and not old_url.startswith("/admin")
and not old_url.startswith("/api")
and not old_url == "/"
):
new_url = old_url[:-1]
return HttpResponseRedirect(new_url)
response = self.get_response(request)
return response