mirror of
https://github.com/Findus23/acronomy.git
synced 2024-09-11 06:23:44 +02:00
don't use trailing slashes in URLs
This commit is contained in:
parent
af6827f91f
commit
cfcbad2ee3
3 changed files with 23 additions and 1 deletions
|
@ -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
0
common/__init__.py
Normal file
21
common/redirect.py
Normal file
21
common/redirect.py
Normal 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
|
Loading…
Reference in a new issue