Showing `Method Not Allowed (GET): /users/logout/`

Method Not Allowed (GET): /users/logout/
Method Not Allowed: /users/logout/
[10/Dec/2023 12:46:21] "GET /users/logout/ HTTP/1.1" 405 0

This is happening when I went to url http://127.0.0.1:8000/users/logout/

Context: I picked the users app from a different project and did all adjustments to fit my project, but this started showing out of nowhere.

1 Like

See Django 4.1 release notes | Django documentation | Django and Deprecation of GET method for LogoutView

6 Likes

you need to use post request, ie

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

source:

3 Likes

So how to implement the solution ?

Answered in the post immediately above yours.

not really, as the issue came up when visiting the url directly, so its not clear where to implement your solution

You don’t / can’t visit the URL directly. That’s not appropriate any more.

(Well, I guess you could create a view that issues a POST to the logout view, but then you’re circumventing the issues fixed by this change.)

In Django 5, LogoutView has been depreciated. So, we need to create our view like this:
@login_required
def user_logout(request):
logout(request)
return render(request, ‘registration/logged_out.html’, {})

In logged_out.html, write the below code.
{% block title %}Logged out{% endblock %}
{% block content %}

Logged out

You have been successfully logged out. You can log-in again.

{% endblock %}
5 Likes

It worked thank you , i was stuck with this error for over an hour

Welcome @shokuyansh !

If you’re having an issue where you would like assistance, please open a new topic for it. This topic has been marked as solved and your post is not likely to attract attention.

When you create your topic, please show the template and view that you are using to handle the logout process. Also include the complete error message being generated.

1 Like

Thanks Bhawna. You’re a life saver!!!

Hi, can I please ask the following: Seeing as it is technically possible for someone to point their browser to the logout view using a GET request, is there a way to simply redirect this to e.g. the homepage all while using a class based view that inherits from LogoutView?

I’m thinking of overriding the http_method_names to include ‘get’ and then adding a ‘get’ method that simply redirects to the homepage. But I imagine its more complicated than that hence the question!

Thanks for any input

Nope, I think that’s about all you’d need to do there.

2 Likes

I’m using Django 4.2

When using a normal get like

<a  href="{% url 'logout' %}">Logout GET ({{ user.get_username }}) </a>

I do not get any error or warning messages.

So has this been allowed again with Django 4.2 ?

No, GET requests for the logout view have been disallowed since Django 5.0. You’re using 4.2.

1 Like

Thank you. This worked.