Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django LogoutView is not working. How to resolve this problem using classbased default function?

I want to use django buildin LogoutView library but it is not working. When i use this, it shows "This page isn’t working" The logout function should work and logout the user. Below is the urls.py code.

from django.urls import path
from .views import TaskList, TaskDetail, TaskCreate, TaskUpdate, DeleteView, CustomLoginView, RegisterPage, TaskReorder
from django.contrib.auth.views import LogoutView

urlpatterns = [
    path('login/', CustomLoginView.as_view(), name='login'),
    path('logout/', LogoutView.as_view(next_page='login'), name='logout'),
    path('register/', RegisterPage.as_view(), name='register'),
    path('', TaskList.as_view(), name='tasks'),
    path('task/<int:pk>/', TaskDetail.as_view(), name='task'),
    path('task-create/', TaskCreate.as_view(), name='task-create'),
    path('task-update/<int:pk>/', TaskUpdate.as_view(), name='task-update'),
    path('task-delete/<int:pk>/', DeleteView.as_view(), name='task-delete'),
    path('task-reorder/', TaskReorder.as_view(), name='task-reorder'),
]
like image 850
mahamudhasan124 Avatar asked Oct 17 '25 20:10

mahamudhasan124


1 Answers

The problem is not the view, the problem is how you make the request. Logging out is a state-changing request, you should not do that with a GET request, since that violates the HTTP protocol. Therefore, since django-5.0, it has to be done through a POST request, with a mini-form:

<form method="post" action="{% url 'logout' %}">
    {% csrf_token %}
    <button type="submit">logout</button>
</form>
like image 84
Willem Van Onsem Avatar answered Oct 19 '25 13:10

Willem Van Onsem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!