相关疑难解决方法(0)

为什么我需要使用@method_decorator来装饰login_required装饰器

我想了解这篇博客文章中发布的mixins的代码.

这些mixin login_required从mixins中调用装饰器django.contrib.auth.decorators,但是它们是由method_decoratorfrom来装饰的django.utils.decorators.在下面的示例代码中,我不明白为什么我需要装饰login_required装饰器.

from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required
class LoginRequiredMixin(object):
    """
    View mixin which verifies that the user has authenticated.

    NOTE:
        This should be the left-most mixin of a view.
    """
    # Why do I need to decorate login_required here
    @method_decorator(login_required)
    def dispatch(self, *args, **kwargs):
        return super(LoginRequiredMixin, self).dispatch(*args, **kwargs) 
Run Code Online (Sandbox Code Playgroud)

method_decorator装饰说它是用来"功能装饰转换为一个方法装饰"但在测试代码,我可以用我的装饰,即使没有method_decorator.

我的装饰师

def run_eight_times(myfunc):
    def inner_func(*args, **kwargs):
        for i in range(8):
            myfunc(*args, **kwargs)
    return inner_func …
Run Code Online (Sandbox Code Playgroud)

django decorator login-required django-class-based-views

10
推荐指数
1
解决办法
6948
查看次数

如果已经通过Django网址登录,则重定向?

目前我使用这些模式登录和注销

urlpatterns += patterns("",
    (r'^login/$', 'django.contrib.auth.views.login', {'template_name': 'login.html'}),
    (r'^logout/$', 'django.contrib.auth.views.logout', {'template_name': 'logout.html'})
)
Run Code Online (Sandbox Code Playgroud)

尽管我的settings.py中有LOGIN_REDIRECT_URL ='/ profile /',但是当我已经登录时,如果我想访问/ login /,Django不会将我发送到/ profile/...

我可以以某种方式重定向auth系统的URL模式吗?我不愿意为此编写自定义视图.

django

10
推荐指数
1
解决办法
6735
查看次数