I'm trying to create the logout functionality on my page. But getting this recursion error, everytime I hit the link that points to the logout url.
Below is the code, for what is suppose to be showing content and a logout functionality if the user is logged in. Otherwise show the login form.
view methods
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%}
console error:
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
Kindly change name of your logout view.
The error is with:
def logout(request):
print('logged out')
logout(request)
return redirect('index')
your view name is logout with request parameter. so when you call logout(request) in the view, it calls your logout view not Django's logout function. that's causing recursion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With