मूल View Class
View class empty toolbox है जिसके ऊपर Django का हर दूसरा class-based view बना है।In this page:
class ViewName(View):
def get(self, request):
return HttpResponse('GET response')
def post(self, request):
return HttpResponse('POST response')
View को Directly Subclass करना
django.views.generic.base से plain View class आपको पूरा control देती है: आप exactly वे HTTP methods define करते हैं जो view class समझती है, get(), post(), या किसी HTTP verb से matching किसी भी lowercase method name को लिखकर।
उदाहरण: Subclassing View Directly
PingView only responds to GET requests; any other method automatically gets a 405 Method Not Allowed response.
from django.http import HttpResponse
from django.views import View
class PingView(View):
def get(self, request):
return HttpResponse('pong')
{# 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. #}
Unsupported Methods के लिए Automatic 405
अगर एक request एक ऐसे HTTP method के साथ आए जो view class ने define नहीं किया (उदाहरण के लिए एक POST जो सिर्फ get() define करने वाले view को भेजा जाए), View.dispatch() एक error raise करने के बजाय अपने आप एक 405 Method Not Allowed response return करता है।
request.method check करने की कभी ज़रूरत नहीं जिन्हें आप support नहीं करते।उदाहरण: Automatic 405 for Unsupported Methods
Sending a POST request to ReadOnlyView returns Django's built-in 405 response without any extra code.
from django.views import View
from django.http import HttpResponse
class ReadOnlyView(View):
def get(self, request):
return HttpResponse('You can only GET this 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. #}
एक View Class में URL Arguments Access करना
Function-based views की तरह ही, class-based views को extra URL keyword arguments (URL pattern में angle brackets से captured) इनके methods के arguments के रूप में मिलते हैं, self और request के बाद।
उदाहरण: Accessing URL Arguments in a View Class
A URL pattern like path("greet/<str:name>/", GreetView.as_view()) passes name into get() automatically.
from django.http import HttpResponse
from django.views import View
class GreetView(View):
def get(self, request, name):
return HttpResponse(f'Hello, {name}!')
{# 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. #}
dispatch()को override करना औरsuper().dispatch(request, *args, **kwargs)call करना भूल जाना, जो method routing को पूरी तरह break कर देता है।- एक method को एक ऐसे नाम से define करना जो Django नहीं पहचानता (जैसे
"handle_get""get"के बजाय), इसलिए यह silently कभी call नहीं होता। http_method_namesमें एक HTTP method को list न करना जब इसे explicitly block या allow करने की ज़रूरत हो।
django.views.generic.base.Viewवह base class है जिससे हर built-in generic CBV inherit करता है।View.as_view()एक callable return करता है जिसे Django का URL dispatcher एक function की तरह invoke कर सकता है।View.dispatch()request.methodदेखता है और matching lowercase method (get, post, put, delete, आदि) call करता है।- एक method जो class पर defined नहीं है simply unsupported है, और Django अपने आप एक HTTP 405 response return करता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: