尝试注销 django 时出现运行时错误

Runtime error when trying to logout django

当我尝试从我的 django 项目注销时,出现以下错误:

"maximum recursion depth exceeded while calling a Python object"

这是注销按钮的url:

url(r'^logout', 'users.views.logout', name='logout'),

视图如下:

from django.shortcuts import render
from deck1.models import Card
from django.template import RequestContext 
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect, HttpResponse
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import login, authenticate, logout
from django.contrib.auth.decorators import login_required
from users.forms import RegisterForm

@login_required
def logout(request):
    logout(request)
    return  HttpResponseRedirect('/deck')

您的视图 logout 正在覆盖内置 logout 函数的命名空间。使用 as 关键字为 django.contrib.auth.login 函数定义别名。

from django.contrib.auth import logout as django_logout

@login_required
def logout(request):
    django_logout(request)
    return  HttpResponseRedirect('/deck')

使用这个:

@login_required
def user_logout(request):
    logout(request)
    return  HttpResponseRedirect('/deck')

并据此更改您的 url

我遇到了同样的问题。请将您的视图函数名称从注销更改为任何其他名称或使用其他名称导入注销。

def logout(request):
    django_logout(request)
    return  HttpResponseRedirect('/deck')

您也可以在代码中简单地更改它来解决这个问题。

from django.contrib.auth import login, authenticate, logout as logoutUser

只需更改这一行,您的代码就会 运行 很好。谢谢