← Back to Django Course | Chapter 9: Class-Based Views | Lesson 2 of 10

मूल View Class

Base View class empty toolbox है जिसके ऊपर Django का हर दूसरा class-based view बना है।
Syntax
markup
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.

markup
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 करता है।

Note: यह safety net मतलब है आपको उन methods के लिए manually 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.

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

markup
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. #}
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. dispatch() को override करना और super().dispatch(request, *args, **kwargs) call करना भूल जाना, जो method routing को पूरी तरह break कर देता है।
  2. एक method को एक ऐसे नाम से define करना जो Django नहीं पहचानता (जैसे "handle_get" "get" के बजाय), इसलिए यह silently कभी call नहीं होता।
  3. 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 करता है।

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.