Logout की Functionality
In this page:
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.
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 होना चाहिए।
उदाहरण: A Safe Logout Button
Wrapping logout in a form with csrf_token makes it a proper POST action instead of an unsafe GET link.
<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/.
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. #}
- Modern Django में logout के लिए एक GET link (
<a href>) उपयोग करना, जब logout करना security के लिए एक POST request होना चाहिए। - Logout के बाद redirect करना भूल जाना, user को एक page पर छोड़ते हुए जो अब भी मानता है कि वे logged in हैं।
- 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 न रहे।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first:
- Introduction to Django's Auth System
- The User Model Overview
- Setting Up a Login View
- Logout Functionality
- User Registration Form
- The login_required Decorator
- The permission_required Decorator
- Django Groups and Permissions
- Password Hashing in Django
- Session Authentication Basics
- Introduction to Custom User Models
- Sending Emails with Django