Search This Blog

Display Views - DetailView - Python

 

Display Views - DetailView

This display view represents the detail of a model object identified by self.object.

Consider the following view that gives the article information. The details are obtained by using the get_context_data method as shown

from django.views.generic.detail import DetailView
...
class ArticleDetailView(DetailView):
    model = Article

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['now'] = timezone.now()
        return context

The view is called in URLconf as below

urlpatterns = [
    path('<slug:slug>/', ArticleDetailView.as_view(), name='article-detail'),
]

The model object can be used as default data context and can be used in html template named as <model>_detail.html.

<p>{{ object.content }}</p>
<p>Reporter: {{ object.reporter }}</p>
<p>Published: {{ object.pub_date|date }}</p>

No comments:

Post a Comment

If you have any doubts, Please let us know.