简体   繁体   English

Django功能要求用户不登录

[英]Django function to require that user not be logged in

I want to do the inverse of @login_required for my login and register views. 我想对登录和注册视图进行@login_required的反操作。 Here is what I want to do (but doesn't work) 这是我想做的(但不起作用)

def amIAuth(request)
    if request.user.is_authenticated():
        return redirect(account)

def login(request):
    amIAuth(request)
    ....

The way you have it doesn't work because login() doesn't do anything with the return value. 您拥有的方式不起作用,因为login()对返回值没有任何作用。

The proper way to do this a decorator, much like @login_required : 进行装饰的正确方法,非常类似于@login_required

def not_loggedin_required(function):
    def wrap(request, *args, **kwargs):
        if request.user.is_authenticated():
            return redirect(account) # redirect to profile page
        else:
            return function(request, *args, **kwargs)
    return wrap

@not_loggedin_required
def login(request):
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM