I'm trying to get rid of trailing slash from urls. Currently the web-server successfully removes slashes and APPEND_SLASH = false Everything works fine but I have a problem with the localized homepage:
http://example.com/en/ - ok http://example.com/en - Page not found (404)
I add this class to Middleware:
from django.shortcuts import redirect from django.utils import translation
class RemoveTrailingSlashFromMultilingualURLsMiddleware: def init(self, get_response): self.get_response = get_response
def __call__(self, request):
response = self.get_response(request)
# Check if the URL ends with a language code and a slash
language_code = translation.get_language()
if request.path.endswith(f'/{language_code}/') and request.path != f'/{language_code}/':
new_path = request.path.rstrip('/')
return redirect(new_path)
and add MIDDLEWARE to setting:
MIDDLEWARE = [
# ... other middleware
'blog.middleware.RemoveTrailingSlashFromMultilingualURLsMiddleware',
# ... other middleware
]
But it doesn't work and keeps giving 404 not found error if possible help me to solve my problem
thanks a lot