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

login_required नामक Decorator

login_required decorator एक locked door sign जैसा है — यह किसी भी ऐसे व्यक्ति को जो logged in नहीं है सीधे login page पर भेज देता है।
Syntax
markup
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.

markup
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 को कहां भेजना है।

Warning: बिना 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.

markup
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.

markup
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. #}
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. settings.py में LOGIN_URL set करना भूल जाना, इसलिए Django यह नहीं ढूंढ पाता कि anonymous users को कहां redirect करना है।
  2. login_required को एक class-based view पर सीधे apply करना बजाय LoginRequiredMixin उपयोग करने के।
  3. हर 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 उपयोग करते हैं।

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.