@login_required为多个视图 [英] @login_required for multiple views

查看:121
本文介绍了@login_required为多个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将会有超过20个意见。所有这些都要求用户首先验证。我必须在每一个人身上放置 @login_required 吗?



https://docs.djangoproject.com/en/1.6 /topics/auth/default/#django.contrib.auth.decorators.login_required

解决方案

我结束了我的npage应用程序目录中的一个新文件称为 lockdown.py ,并将代码从这个解决方案:

  import re 

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


class RequireLoginMiddleware(object):

包含login_required装饰器的中间件组件围绕
匹配的URL模式。要使用,将类添加到MIDDLEWARE_CLASSES,

settings.py中定义LOGIN_REQUIRED_URLS和LOGIN_REQUIRED_URLS_EXCEPTIONS。例如:
------
LOGIN_REQUIRED_URLS =(
r'/ topsecret /(.*)$',

LOGIN_REQUIRED_URLS_EXCEPTIONS =(
r'/ topsecret / login(。*)$',
r'/ topsecret / logout(。*)$',

------
LOGIN_REQUIRED_URLS是您定义网址格式的位置;每个模式必须
是一个有效的正则表达式。

LOGIN_REQUIRED_URLS_EXCEPTIONS相反,您明确地在
中定义任何异常(如登录和注销URL)。

def __init __(self):
self.required = tuple(re.compile(url)for url in settings.LOGIN_REQUIRED_URLS)
self.exceptions = tuple

def process_view(self,request,view_func,view_args,view_kwargs):
#如果用户已经登录,则不需要处理URL
如果request.user.is_authenticated():
返回无

#异常匹配应立即返回无
的url在self.exceptions:
如果url.match(request.path):
return None

#返回匹配受限URL模式的请求
#用login_required装饰器
包装为url self.required:
如果url.match(request.path):
返回login_required(view_func)(请求,* view_args,** view_kwargs)

#明确地重试n不适用于所有不匹配的请求
返回无

之后在 settings.py 我将其添加到 MIDDLEWARE_CLASSES ...

  MIDDLEWARE_CLASSES =(
#...
'npage.lockdown.RequireLoginMiddleware',

当然,这些行将锁定整个网站:

  LOGIN_REQUIRED_URLS =(
r'/(.*)$',

LOGIN_REQUIRED_URLS_EXCEPTIONS =(
r'/ login(。*)$',
r '/logout(.*)$',


I am going to have probably over 20 views. All of them require the user to authenticate first. Do I have to put @login_required over each one or is there a better way?

https://docs.djangoproject.com/en/1.6/topics/auth/default/#django.contrib.auth.decorators.login_required

解决方案

I ended up making a new file in my npage app directory called lockdown.py and pasted the code from this solution:

import re

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


class RequireLoginMiddleware(object):
    """
    Middleware component that wraps the login_required decorator around
    matching URL patterns. To use, add the class to MIDDLEWARE_CLASSES and
    define LOGIN_REQUIRED_URLS and LOGIN_REQUIRED_URLS_EXCEPTIONS in your
    settings.py. For example:
    ------
    LOGIN_REQUIRED_URLS = (
        r'/topsecret/(.*)$',
    )
    LOGIN_REQUIRED_URLS_EXCEPTIONS = (
        r'/topsecret/login(.*)$',
        r'/topsecret/logout(.*)$',
    )
    ------
    LOGIN_REQUIRED_URLS is where you define URL patterns; each pattern must
    be a valid regex.

    LOGIN_REQUIRED_URLS_EXCEPTIONS is, conversely, where you explicitly
    define any exceptions (like login and logout URLs).
    """
    def __init__(self):
        self.required = tuple(re.compile(url) for url in settings.LOGIN_REQUIRED_URLS)
        self.exceptions = tuple(re.compile(url) for url in settings.LOGIN_REQUIRED_URLS_EXCEPTIONS)

    def process_view(self, request, view_func, view_args, view_kwargs):
        # No need to process URLs if user already logged in
        if request.user.is_authenticated():
            return None

        # An exception match should immediately return None
        for url in self.exceptions:
            if url.match(request.path):
                return None

        # Requests matching a restricted URL pattern are returned
        # wrapped with the login_required decorator
        for url in self.required:
            if url.match(request.path):
                return login_required(view_func)(request, *view_args, **view_kwargs)

        # Explicitly return None for all non-matching requests
        return None

After that in settings.py I added this to MIDDLEWARE_CLASSES...

MIDDLEWARE_CLASSES = (
    # ...
    'npage.lockdown.RequireLoginMiddleware',
)

And of course, these lines to lock the whole site down:

LOGIN_REQUIRED_URLS = (
        r'/(.*)$',
    )
LOGIN_REQUIRED_URLS_EXCEPTIONS = (
    r'/login(.*)$',
    r'/logout(.*)$',
)

这篇关于@login_required为多个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆