← Back to Django Course | Chapter 10: Authentication & Authorization | Lesson 4 of 12

Logout की Functionality

Logout करना अपना visitor badge वापस handing back करने जैसा है — Django भूल जाता है कि आप कौन हैं जब तक आप फिर से sign in न करें।
Syntax
markup
from django.contrib.auth import logout

def logout_view(request):
    logout(request)
    return redirect('url_name')

Logout View

logout() user की session data clear करता है, इसलिए next request पर request.user AnonymousUser बन जाता है।

उदाहरण: The Logout View

logout(request) only needs the request object — it looks up and clears the active session itself.

markup
from django.contrib.auth import logout
from django.shortcuts import redirect

def logout_view(request):
    logout(request)
    return redirect('login')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

एक Safe Logout Button

Logout करना server state बदलता है, इसलिए यह plain GET link के बजाय एक POST form से trigger होना चाहिए।

Warning: एक GET-based logout link prefetching या crawlers द्वारा accidentally trigger हो सकता है।

उदाहरण: A Safe Logout Button

Wrapping logout in a form with csrf_token makes it a proper POST action instead of an unsafe GET link.

markup
<form method="post" action="{% url 'logout' %}">
  {% csrf_token %}
  <button type="submit">Log Out</button>
</form>
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Logout URL Wire करना

Logout view को अपना URL चाहिए ताकि ऊपर वाला form इस पर post कर सके।

उदाहरण: Wiring the Logout URL

Naming it logout lets the template's {% url logout %} tag resolve to /logout/.

markup
from django.urls import path
from . import views

urlpatterns = [
    path('logout/', views.logout_view, name='logout'),
]
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Modern Django में logout के लिए एक GET link (<a href>) उपयोग करना, जब logout करना security के लिए एक POST request होना चाहिए।
  2. Logout के बाद redirect करना भूल जाना, user को एक page पर छोड़ते हुए जो अब भी मानता है कि वे logged in हैं।
  3. Logout button दिखाने से पहले request.user.is_authenticated न जांचना, पहले से logged-out users को इसे दिखाते हुए।
चैप्टर सारांश
  • django.contrib.auth.logout() current session clear करता है, login खत्म करते हुए।
  • Modern Django logout के लिए एक POST request (जैसे एक submit button वाला form) recommend करता है, plain GET link नहीं।
  • Logout के बाद हमेशा किसी दूसरे page पर redirect करें ताकि user एक stale view पर stuck न रहे।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.