Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django函数要求用户不登录_Python_Django_Authentication - Fatal编程技术网

Python Django函数要求用户不登录

Python Django函数要求用户不登录,python,django,authentication,Python,Django,Authentication,我想对我的登录和注册视图执行@login\u required的相反操作。 以下是我想做的(但不起作用) 您拥有它的方式不起作用,因为login()对返回值没有任何作用 正确的方法是使用decorator,很像@login\u required: def not_loggedin_required(function): def wrap(request, *args, **kwargs): if request.user.is_authenticated():

我想对我的登录和注册视图执行@login\u required的相反操作。 以下是我想做的(但不起作用)


您拥有它的方式不起作用,因为login()对返回值没有任何作用

正确的方法是使用decorator,很像@login\u 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):
    ...

您拥有它的方式不起作用,因为login()对返回值没有任何作用

正确的方法是使用decorator,很像@login\u 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):
    ...

它以什么方式不起作用(它显示了什么行为),您所说的“需要登录的反向”是什么意思?您应该能够编写自己的decorator“@”函数;搜索如何做到这一点,或者希望有人知道如何回应你。它以什么方式不起作用(它显示了什么行为),以及“所需登录的反向”是什么意思?你应该能够编写自己的decorator“@”函数;搜索如何做到这一点,或者希望有人知道如何回应你。我必须在哪里写不需要的日志?我必须导入一些东西吗?您可以将其写入login()和register()视图上方的同一个文件中(如我在这里所示),也可以将其写入单独的文件(例如decorators.py),并在顶部导入“from decorators import not_loggedin_required”我写的和你写的一模一样,当我写日志时,我仍然可以访问我的登录视图。你能试着把打印语句或其他形式的日志放到if和else分支中,看看发生了什么吗?@TotuDoum抱歉,我只是编辑来修复一个严重的错误。你能试一下现在写的答案吗?我必须在哪里写不需要的日志?我必须导入一些东西吗?您可以将其写入login()和register()视图上方的同一个文件中(如我在这里所示),也可以将其写入单独的文件(例如decorators.py),并在顶部导入“from decorators import not_loggedin_required”我写的和你写的一模一样,当我写日志时,我仍然可以访问我的登录视图。你能试着把打印语句或其他形式的日志放到if和else分支中,看看发生了什么吗?@TotuDoum抱歉,我只是编辑来修复一个严重的错误。你能试一下现在写的答案吗?