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/
urls.py:
from django.contrib.auth import views as auth_views
urlpatterns = [
...other urls...
path('users/logout/', auth_views.LogoutView.as_view(), name='logout'),
]
I am expecting user to logout
1
Since django-5, you need to do this through a POST request, since it has side-effects. The fact that it worked with a GET request was (likely) a violation of the HTTP protocol: it made it possible for certain scripts to log out users, without the user wanting to. So a POST request also protects against cross-site request forgery (CSRF) [wiki].
So in the template, work with a mini-form:
<form method="post" action="{% url 'logout' %}">
{% csrf_token %}
<button type="submit">logout</button>
</form>
5
i was facing the same issue but i solve this with following code
<form id="frm_logout" method="post" action="{% url 'logout' %}"> {% csrf_token %} <a href="javascript:$('#frm_logout').submit();" class="px-5 py-3 rounded-xl text-white bg-teal-600 hover:bg-teal-700">logout</a> </form>
this line you write before ending the body tag
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
Since Django-5, you need to logout through a POST request
<li>
<form id="logout-form" action="{% url 'logout' %}" method="post">
<a href="#" onclick="document.getElementById('logout-form').submit(); return false;">Sign out</a>
{% csrf_token %}
</form>
</li>
<a class="nav-item nav-link" href="#" onclick="logout()">Logout</a>
<form id="logout-form" action="{% url 'logout' %}"method="post" style="display: none;">
{% csrf_token %}
</form>
<script>
function logout() {
var form = document.getElementById('logout-form');
form.submit();
}
</script
1