DetailView की मूल बातें
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".
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).
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.
<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. #}
- यह भूल जाना कि DetailView को कौन सा object fetch करना है यह जानने के लिए URL pattern में एक
pkयाslugचाहिए। - यह मान लेना कि context variable हमेशा
"object"है जबcontext_object_nameकहीं customize किया गया हो। - किसी matching row न वाले queryset पर DetailView उपयोग करना और एक crash के बजाय automatic 404 से surprised होना।
- DetailView एक single object दिखाता है, URL से एक
pkयाslugkeyword argument उपयोग करके matched। - Object template में
objectके रूप में याcontext_object_nameसे set किए नाम के रूप में available है। - एक missing object एक unhandled exception के बजाय अपने आप
Http404raise करता है। template_nameexplicitly set न होने पर<app>/<model>_detail.htmlको default करता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: