Python 在自定义装饰器中区分未经身份验证的用户

Python 在自定义装饰器中区分未经身份验证的用户,python,django,Python,Django,这里是Django初学者 我一直在使用内置的login\u requireddecorator。我想为某些用户覆盖它,这些用户的参考URL与特定模式匹配(例如,来自/buy\u和\u sell/的所有用户) 我的目的是只向这些用户显示一个特殊的登录页面,向其他所有用户显示一个通用的登录页面 我一直在看各种编写定制装饰器的例子(例如,和)。但我发现对于初学者来说,这些定义很难理解。有人能给我一个外行的理解(最好是说明性的例子)我如何解决我的问题吗?Django中包含decorator。你不必自己制

这里是Django初学者

我一直在使用内置的
login\u required
decorator。我想为某些用户覆盖它,这些用户的参考URL与特定模式匹配(例如,来自
/buy\u和\u sell/
的所有用户)

我的目的是只向这些用户显示一个特殊的登录页面,向其他所有用户显示一个通用的登录页面

我一直在看各种编写定制装饰器的例子(例如,和)。但我发现对于初学者来说,这些定义很难理解。有人能给我一个外行的理解(最好是说明性的例子)我如何解决我的问题吗?

Django中包含decorator。你不必自己制作decorator

from django.contrib.auth.decorators import user_passes_test

def check_special_user(user):
    return user.filter(is_special=True)

# if not the special user it will redirect to another login url , otherwise process the view
@user_passes_test(check_special_user,login_url='/login/') 
def my_view(request):
   pass
    ...
装饰师中的需求请求

要做到这一点,请在项目或应用程序中创建的克隆版本,并进行如下更改:

def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
    """
    Decorator for views that checks that the user passes the given test,
    redirecting to the log-in page if necessary. The test should be a callable
    that takes the user object and returns True if the user passes.
    """

    def decorator(view_func):
        @wraps(view_func, assigned=available_attrs(view_func))
        def _wrapped_view(request, *args, **kwargs):
            if test_func(request.user):  # change this line to request instead of request.user
                return view_func(request, *args, **kwargs)
            path = request.build_absolute_uri()
            resolved_login_url = resolve_url(login_url or settings.LOGIN_URL)
            # If the login url is the same scheme and net location then just
            # use the path as the "next" url.
            login_scheme, login_netloc = urlparse(resolved_login_url)[:2]
            current_scheme, current_netloc = urlparse(path)[:2]
            if ((not login_scheme or login_scheme == current_scheme) and
                    (not login_netloc or login_netloc == current_netloc)):
                path = request.get_full_path()
            from django.contrib.auth.views import redirect_to_login
            return redirect_to_login(
                path, resolved_login_url, redirect_field_name)
        return _wrapped_view
    return decorator
将test_func(request.user)更改为test_func(request),您将获得 整个要求在您的装饰功能

编辑:在url.py中

url (
    r'^your-url$',
    user_passes_test(check_special_user, login_url='/login/')(
        my_view
    ),
    name='my_view'
)

下面是理解python装饰器的最佳答案:

您可以使用以下的
login\u url
参数:

另一种方法是创建自定义装饰器,例如:


不管怎样,我给你+1作为你的答案。问题:我怎样才能在
check\u special\u user
中获得推荐url?我没有
请求
在那里。没有请求不能用于用户\u通过\u测试方法装饰器。你到底想做什么?正如我在问题中提到的,我正试图向未经授权的用户显示一个特殊的登录页面,这些用户来自我应用程序中的某些URL(例如,
/buy\u and\u sell/
)。所有其他未经授权的用户将显示一个通用登录页面。为此,我需要查看
request.META.get('HTTP\u REFERER')
的内容。“试图向来自特定URL的用户显示一个特殊的登录页面”-您是否为此在用户表中进行了任何更改?就像用户是现在发起的一样。不,在此流程中用户未经身份验证,我在用户表中没有更改任何内容。只想向这组特殊的未授权用户显示一个特殊的登录页面,并向所有其他未授权用户显示一个通用的登录页面。
@login_required(login_url='some_url)
from django.contrib.auth.decorators import user_passes_test

def email_check(user):
    return user.email.endswith('@example.com')

@user_passes_test(email_check)
def my_view(request):
    ...