我试图在我的页面上创建注销功能。但每次我点击指向注销网址的链接时,都会收到此递归错误。
以下是代码,用于显示内容和用户登录时的注销功能。否则显示登录表单。
查看方法
def login(request):
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
context = {
}
if user is not None:
print('Correct user'
else:
print('Wrong user')
return render(request, 'index.html', context)
def logout(request):
print('logged out')
logout(request)
return redirect('index')
URL
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^login/$', views.login, name='login'),
url(r'^logout/$', views.logout, name='logout'),
]
的index.html
{% if user %}
<h3>Hello {{user.username}}</h3>
<a href="{% url 'crowd:logout' %}">Logout</a>
{% else %}
<form action="{% url 'crowd:login' %}" method='post'>
{% csrf_token %}
<input class='logintext' type='text' placeholder='Username' name='username'>
<input class='logintext' type='password' placeholder='Password' name='password'><br>
<input class='loginbutton' type='submit' value='Login'>
<a class='loginforgotpassword' href="{% url 'crowd:register' %}"> Forgot password?</a>
<a class='loginregister' href="{% url 'crowd:register' %}"> Register</a>
</form>
{% endif%}
控制台错误:
File "C:\Users\Rasmus\workspace\Crowd\src\Cr\views.py", line 48, in logout
logout(request)
File "C:\Users\Rasmus\workspace\Crowd\src\Cr\views.py", line 48, in logout
logout(request)
File "C:\Users\Rasmus\workspace\Crowd\src\Cr\views.py", line 48, in logout
logout(request)
File "C:\Users\Rasmus\workspace\Crowd\src\Cr\views.py", line 47, in logout
print('logged out')
RecursionError: maximum recursion depth exceeded
[04/Sep/2016 23:35:59] "GET /Crowd/logout/ HTTP/1.1" 500 3210436
答案 0 :(得分:1)
请更改退出视图的名称。
错误在于:
def logout(request):
print('logged out')
logout(request)
return redirect('index')
您的视图名称是带有请求参数的注销。因此,当您在视图中调用logout(request)时,它会调用您的注销视图而不是Django的注销功能。这导致了递归。