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

DetailView की मूल बातें

DetailView अपने आप एक single page बनाता है जो एक specific item को पूरे detail में दिखाता है, सिर्फ shelf देखने के बजाय एक book खोलकर इसका page पढ़ने जैसा।
Syntax
markup
from django.views.generic import DetailView

class ModelNameDetailView(DetailView):
    model = ModelName

# urls.py
path('route/<int:pk>/', ModelNameDetailView.as_view(), name='url_name')

एक Minimal DetailView

DetailView को एक model और एक URL pattern चाहिए जो pk या slug capture करे, ताकि यह exactly जान सके कि कौन सी single row fetch और display करनी है।

उदाहरण: A Minimal DetailView

BookDetailView fetches one Book row using the pk from the URL and passes it to the template as "book".

markup
from django.views.generic import DetailView
from .models import Book


class BookDetailView(DetailView):
    model = Book
    template_name = 'books/book_detail.html'
    context_object_name = 'book'
{# 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. #}

URL Pattern Match करना

URL pattern में एक <int:pk> या <slug:slug> converter शामिल होना चाहिए जो यह match करे कि DetailView कैसे object lookup करता है, नहीं तो Django view चलने पर एक error raise करता है।

उदाहरण: Matching the URL Pattern

The <int:pk> segment supplies the primary key DetailView uses internally to run Book.objects.get(pk=pk).

markup
from django.urls import path
from .views import BookDetailView

urlpatterns = [
    path('books/<int:pk>/', BookDetailView.as_view(), name='book-detail'),
]
{# 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. #}

Template में Object Details Render करना

एक बार DetailView object ढूंढ ले, इसके fields context variable पर dot notation के through template में accessible हैं।

उदाहरण: Rendering Object Details in a Template

Each {{ book.field }} expression reads an attribute straight off the single Book instance DetailView supplied.

markup
<h1>{{ book.title }}</h1>
<p>Author: {{ book.author }}</p>
<p>Published: {{ book.published_date }}</p>
{# 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. यह भूल जाना कि DetailView को कौन सा object fetch करना है यह जानने के लिए URL pattern में एक pk या slug चाहिए।
  2. यह मान लेना कि context variable हमेशा "object" है जब context_object_name कहीं customize किया गया हो।
  3. किसी matching row न वाले queryset पर DetailView उपयोग करना और एक crash के बजाय automatic 404 से surprised होना।
चैप्टर सारांश
  • DetailView एक single object दिखाता है, URL से एक pk या slug keyword argument उपयोग करके matched।
  • Object template में object के रूप में या context_object_name से set किए नाम के रूप में available है।
  • एक missing object एक unhandled exception के बजाय अपने आप Http404 raise करता है।
  • template_name explicitly set न होने पर <app>/<model>_detail.html को default करता है।

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.