python - Django 函数要求用户不登录

标签 python django authentication

我想为我的登录和注册 View 执行与 @login_required 相反的操作。 这是我想做的(但不起作用)

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

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

最佳答案

您的方法不起作用,因为 login() 不对返回值执行任何操作。

装饰器的正确方法,很像@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):
    ...

关于python - Django 函数要求用户不登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27968184/

相关文章:

aws EMR 中的 python 虚拟环境

python - (管理员)内联的表单字段 ||无法导入 InlineModelAdmin

Django-filter 按相关字段过滤

python - 为 UserProfile 指定的未知字段(用户名) - Django

asp.net-mvc - ASP.NET MVC 表单例份验证和未经身份验证的 Controller 操作

Python - 使用ffprobe获取视频的持续时间

python - 将具有相同 id 的行分组,pandas/python

authentication - Symfony2 : Login via user or IP

c# - 即使在与服务器进行连续事件后,Owin token 也会过期

Python Queue 在 get() 后不会释放内存