如何在没有身份验证的情况下对Django中除了一个URL之外的所有URL进行密码保护?

如何在没有身份验证的情况下对Django中除了一个URL之外的所有URL进行密码保护?,django,Django,是否有一个工具允许我在Django中对除一个URL以外的所有URL进行密码保护而无需身份验证?您可以为此编写一个自定义: class LoggedInUserCheckMiddleware(object): def __init__(self, get_response): self.get_response = get_response def __call__(self, request): full_path = request.get_f

是否有一个工具允许我在Django中对除一个URL以外的所有URL进行密码保护而无需身份验证?

您可以为此编写一个自定义:

class LoggedInUserCheckMiddleware(object):
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        full_path = request.get_full_path()
        if not full_path == '/no-need-auth-url' and not request.user.is_authenticated:
           raise Http404

        response = self.get_response(request)
        return response
并将其添加到设置中的
中间件

MIDDLEWARE = [
    # rest of middlewares
    'path.to.LoggedInUserCheckMiddleware'
]

您可以根据需要使用@loging\u装饰器

from django.contrib.auth.decorators import login_required
在除一个视图/url之外的所有视图上