[Answered ]-Django: Limit user's session by time

2👍

Changing the auth backend is probably not the solution you are looking for (at least I wouldn’t recommend it), since you are changing security-critical parts of your application.

I would suggest a custom middleware: If registered users trying to access your site between 8am and 5pm, they’ll see a warning that this site cannot be used.

from django.utils import timezone
from django.core.exceptions import PermissionDenied

class AccessRestrictionMiddleware:
    def process_request(self, request):
        current_hour = timezone.now().hour
        is_time_restricted = current_hour >= 8 and current_hour < 17
        if request.user.is_authenticated() and is_time_restricted:
           raise PermissionDenied

Leave a comment