← Back to Django Course | Chapter 2: Apps, URLs & Views | Lesson 10 of 10

View Decorators की मूल बातें

एक decorator एक view के सामने रखा गया security guard जैसा है — यह view run होने देने से पहले कुछ (जैसे कौन सा HTTP method उपयोग हुआ) जांचता है।
Syntax
markup
from django.views.decorators.http import require_GET, require_http_methods

@require_http_methods(['GET', 'POST'])
def view_name(request):
    return HttpResponse('content')

एक Decorator क्या करता है

एक decorator एक Python function है जो किसी दूसरे function को wrap करता है उसका code बदले बिना extra behavior जोड़ने के लिए। Django common view concerns के लिए कई decorators देता है, जैसे HTTP methods restrict करना।

उदाहरण: What a Decorator Does

A decorator is a Python function that wraps another function to add extra behavior without changing its code. Django ships several decorators for common view concerns, like restricting HTTP methods.

markup
from django.views.decorators.http import require_GET
from django.http import HttpResponse

@require_GET
def home(request):
    return HttpResponse('GET-only view')
{# 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. #}

Specific Methods तक Restrict करना

require_http_methods allowed HTTP verbs की एक list लेता है; कोई भी दूसरा verb एक automatic 405 Method Not Allowed response पाता है, इसलिए view body को खुद request.method कभी जांचना नहीं पड़ता।

उदाहरण: Restricting to Specific Methods

require_http_methods takes a list of allowed HTTP verbs; any other verb gets an automatic 405 Method Not Allowed response, so the view body never has to check request.method itself.

markup
from django.views.decorators.http import require_http_methods
from django.http import HttpResponse

@require_http_methods(['GET', 'POST'])
def contact(request):
    return HttpResponse('Contact page')
{# 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. #}

Multiple Decorators Stack करना

Decorators stack किए जा सकते हैं; Python उन्हें bottom-up apply करता है, इसलिए function के सबसे पास वाला decorator view call होने पर पहले run होता है।

Note: Order मायने रखता है — stacked decorators को execution order समझने के लिए bottom से ऊपर पढ़ें।

उदाहरण: Stacking Multiple Decorators

Decorators can be stacked; Python applies them bottom-up, so the decorator closest to the function runs first when the view is called.

markup
from django.views.decorators.http import require_GET
from django.views.decorators.cache import never_cache

@never_cache
@require_GET
def dashboard(request):
    return HttpResponse('Live dashboard data')
{# 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. एक view पर @require_POST apply करना जिसे GET requests भी handle करनी हैं, जो सभी GET requests को fail करने का कारण बनता है।
  2. उपयोग करने से पहले decorator को django.views.decorators.http से import करना भूल जाना।
  3. Decorators को गलत order में stack करना, जो बदल सकता है वे view function के साथ कैसे interact करते हैं।
चैप्टर सारांश
  • Decorators एक view function को wrap करते हैं behavior जोड़ने के लिए, जैसे allowed HTTP methods restrict करना।
  • @require_http_methods, @require_GET, और @require_POST restrict करते हैं कि एक view कौन से HTTP verbs accept करता है।
  • Decorators सीधे view function definition के ऊपर @ syntax उपयोग करके apply किए जाते हैं।

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.