diff --git a/boards/middlewares.py b/boards/middlewares.py --- a/boards/middlewares.py +++ b/boards/middlewares.py @@ -6,6 +6,8 @@ from django.utils import timezone from boards import utils from boards.models import Ban +PATH_BANNED = '/banned/' + SESSION_TIMEZONE = 'django_timezone' RESPONSE_CONTENT_TYPE = 'Content-Type' @@ -25,14 +27,15 @@ class BanMiddleware: def __call__(self, request): response = self.get_response(request) - if request.path != '/banned/': + if request.path != PATH_BANNED: ip = utils.get_client_ip(request) - bans = Ban.objects.filter(ip=ip) + try: + ban = Ban.objects.get(ip=ip) - if bans.exists(): - ban = bans[0] if not ban.can_read: return redirect('banned') + except Ban.DoesNotExist: + pass return response