Add a Setting to Exempt URLs in `LoginRequiredMiddleware`

I think this is the way. Here’s an example implementation of a backwards compatible decorator for views:

try:
    from django.contrib.auth.decorators import login_not_required
except ImportError:
    # For Django < 5.1, copy the current Django implementation
    def login_not_required(view_func):
        """
        Decorator for views that allows access to unauthenticated requests.
        """
        view_func.login_required = False
        return view_func

I’ve added this to projects I maintain, for example here.

I think there’s a lot of hesitation these days to adding new settings, especially when adding the relevant code to your own project seems like a sensible way to go.

4 Likes