Creating a Logout functionality in Django
Updated on Oct. 8, 2017 |
In this tutorial, we are going to learn about how to deploy logout functionality in Django based website. You can simply use built-in module to destroy session to logging out a user. Let’s start!
Step 1: In html template, create a link without directing to ‘/logout/’.
Step 2: In views.py, create the logout(request) function as below.
from django.contrib.auth import logout as site_logout
site_logout(request) clears all session data of user who logged in previously and prevents others from using the session data. In final line, we redirect to a URL after logout (here, we want to redirect to ‘/login/’).
Step 3: In urls.py, create the logout URL as below, where you need to call the views.logout function that you created.
from . import views
urlpatterns = [
It’s Done! Now, your logout functionality should work.