2

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.

3
  • You use a POST request. Can you share the template where you have a log out button? Commented Dec 19, 2023 at 10:25
  • {% if user.is_authenticated %} <a href="{% url 'logout' %}">Log In</a> Commented Dec 20, 2023 at 5:46
  • Already answered here stackoverflow.com/questions/77690729/…. Commented Feb 2, 2024 at 11:32

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

-1

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

1 Comment

This didn't work for me (I imported from django.contrib.auth import views as auth_views). It got me to admin logout page, and didn't log out at all.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.