Skip to content

Logout

In this video, we are going to learn how to implement the feature of logout in django.

Views

In the views.py file, at first we need to import logout method from contrib.auth

from django.contrib.auth import (logout as user_logout,)

Now, create a view named logout.

def logout(request):
    # Peform the logout using user_logout method.
    user_logout(request)
    # Now, inform the user that he/she is logged out successfully.
    messages.info(request, "Logged out successfully")
    # After logging out, redirect the user to the login page.
    return HttpResponseRedirect(reverse("accounts:login"))

URLS

Lets add a url for this logout view.

path("logout/", views.logout, name="logout"),

[Run the development server and see the changes.]