Search code examples
djangoauthenticationsecuritydjango-viewsdjango-urls

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'),
]

Solution

  • I use Logout in my projects as follows:

    # app/urls.py
    
    from django.urls import path from . import views
    
    urlpatterns = [
        path('logout', views.LogoutView.as_view(), name='logout'), ]
    

    and in views.py

    #app/views.py
    
    from django.contrib.auth import logout
    
    class LogoutView(View):
        def get(self, request):
            logout(request)
            return redirect('login')
    

    I hope it was useful for you.