View Decorators की मूल बातें
In this page:
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.
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.
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 होता है।
उदाहरण: 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.
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. #}
- एक view पर @require_POST apply करना जिसे GET requests भी handle करनी हैं, जो सभी GET requests को fail करने का कारण बनता है।
- उपयोग करने से पहले decorator को django.views.decorators.http से import करना भूल जाना।
- 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 किए जाते हैं।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: