Saeed
Saeed

Reputation: 89

Problem with Django class-based LogoutView in Django 5.0

I used Django class-based login and logout views by "django.contrib.auth.urls", login feature works correctly, but when logging out via the link "http://127.0.0.1:8000/accounts/logout" it returns an http 405 error page, saying "method not allowed (GET)". I found out this logout feature was available in django 4.0 but in django 5.0 it's removed.

Support for logging out via GET requests in the django.contrib.auth.views.LogoutView and django.contrib.auth.views.logout_then_login() is removed. -djangoproject.com

How can I fix it?

I think I should somehow make it use POST request instead GET request when loging out but don't know how.

Upvotes: 2

Views: 3391

Answers (2)

Starodub Paul
Starodub Paul

Reputation: 1

path( "logout/", auth_views.LogoutView.as_view( http_method_names=["post", "get", "options"] ),

Upvotes: -1

Saeed
Saeed

Reputation: 89

The problem was with the request method used, my initial template used GET method:

{% if user.is_authenticated %} 
<a href="{% url 'logout' %}">Log In</a>

but by editing the template to this:

<form action="{% url 'logout' %}" method="post">
    {% csrf_token %}
    <button type="submit">Log out</button>
</form>

it worked.

Upvotes: 6

Related Questions