login_required नामक Decorator
login_required decorator एक locked door sign जैसा है — यह किसी भी ऐसे व्यक्ति को जो logged in नहीं है सीधे login page पर भेज देता है।from django.contrib.auth.decorators import login_required
@login_required
def view_name(request):
return render(request, 'template.html')
# settings.py
LOGIN_URL = '/login/'
एक View को Protect करना
@login_required view body run होने से पहले request.user.is_authenticated जांचता है, anonymous users को इसके बजाय redirect करते हुए।
उदाहरण: Protecting a View
If the visitor isn't logged in, Django redirects them to LOGIN_URL before dashboard() ever runs.
from django.contrib.auth.decorators import login_required
from django.shortcuts import render
@login_required
def dashboard(request):
return render(request, 'dashboard.html')
{# 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. #}
LOGIN_URL Set करना
LOGIN_URL Django को बताता है कि protected view hit करने वाले anonymous users को कहां भेजना है।
LOGIN_URL के, Django /accounts/login/ पर default करता है, जो आपके project में मौजूद न हो।उदाहरण: Setting LOGIN_URL
Setting this to the URL name login means Django resolves it to wherever that named URL actually points.
LOGIN_URL = '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. #}
Login के बाद Return करना
Django login redirect में ?next=/dashboard/ append करता है ताकि login view बाद में user को वापस भेज सके।
उदाहरण: Returning After Login
Reading next from the submitted form sends the user back to the page they originally tried to visit.
from django.contrib.auth import authenticate, login
from django.shortcuts import redirect, render
def login_view(request):
if request.method == 'POST':
user = authenticate(
request, username=request.POST['username'],
password=request.POST['password'],
)
if user:
login(request, user)
return redirect(request.POST.get('next', 'home'))
return render(request, 'login.html')
{# 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. #}
settings.pyमेंLOGIN_URLset करना भूल जाना, इसलिए Django यह नहीं ढूंढ पाता कि anonymous users को कहां redirect करना है।login_requiredको एक class-based view पर सीधे apply करना बजायLoginRequiredMixinउपयोग करने के।- हर view में manually 'if not request.user.is_authenticated: redirect(...)' लिखना बजाय decorator उपयोग करने के।
- @login_required एक view को wrap करता है ताकि सिर्फ authenticated users इसे access कर सकें।
- Anonymous visitors LOGIN_URL पर redirect होते हैं, एक ?next= parameter के साथ जो login के बाद उन्हें वापस भेजता है।
- यह function-based views पर apply होता है; class-based views इसके बजाय LoginRequiredMixin उपयोग करते हैं।
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