可以通过应用于整个应用程序login_required吗?

时间:2010-03-18 06:21:15

标签: django

有没有办法将login_required装饰器应用到整个应用程序?当我说“app”时,我的意思是在django意义上,也就是说一组网址和视图,而不是整个项目。

5 个答案:

答案 0 :(得分:5)

是的,你应该使用中间件。

尝试查看具有一些差异的解决方案:

答案 1 :(得分:1)

我认为您正在寻找this snippet,其中包含需要登录的middleware

答案 2 :(得分:1)

这是一个老问题。但是这里有:

Django Decorator Include

这是URLConf中include的替代。将login_required应用于整个应用程序的效果。

答案 3 :(得分:0)

我想知道是否有任何解决方案可以使它像这样工作:

/app/app.py
class AppConfig(AppConfig):
    login_required = True

/project/urls.py
urlpatterns = [
    url(r'app/', include('app.urls', namespace='app'))
]

/common/middleare.py
def LogMiddleware(get_response):
    def middleware(request):
        # solution 1
        app = get_app(request)
        if app.login_required is True and request.is_authenticated is Fasle:
            return HttpResponseRedirect(redirect_url)
        # solution 2
        url_space = get_url_space(request.get_raw_uri())
        if url_space.namespace in ['app', 'admin', 'staff', 'manage'] and \
                request.is_authenticated is False:
            return HttpResponseRedirect(redirect_url)

我将检查是否有任何方法可以获取请求的应用或网址名称。我认为它看起来更漂亮。

答案 4 :(得分:0)

从Django 3+开始,您可以通过应用中间件将login_require()设置为整个应用程序。喜欢以下内容:

步骤1:在yourapp目录中创建一个新文件any.py,并编写以下内容:

import re
from django.conf import settings
from django.contrib.auth.decorators import login_required

//for registering a class as middleware you at least __init__() and __call__()
//for this case we additionally need process_view() which will be automatically called by Django before rendering a view/template

class ClassName(object):
    
    //need for one time initialization, here response is a function which will be called to get response from view/template
    def __init__(self, response):
        self.get_response = response
        self.required = tuple(re.compile(url) for url in settings.AUTH_URLS)
        self.exceptions = tuple(re.compile(url)for url in settings.NO_AUTH_URLS)

    def __call__(self, request):
        //any code written here will be called before requesting response
        response = self.get_response(request)
        //any code written here will be called after response
        return response

    //this is called before requesting response
    def process_view(self, request, view_func, view_args, view_kwargs):
        //if authenticated return no exception
        if request.user.is_authenticated:
            return None
        //return login_required()
        for url in self.required:
            if url.match(request.path):
                return login_required(view_func)(request, *view_args, **view_kwargs)
        //default case, no exception
        return None

第2步:将以下内容添加到project / settings.py中的Middleware []中,如下所示

MIDDLEWARE = [
    // your previous middleware
    'yourapp.anything.ClassName',
]

第3步:还将以下代码段添加到project / settings.py

AUTH_URLS = (
    //disallowing app url, use the url/path that you added on mysite/urls.py (not myapp/urls.py) to include as your app urls
    r'/your_app_url(.*)$',
)