views.generic.detail.DetailView

class django.views.generic.detail.DetailView

While this view is executing, self.object will contain the object that the view is operating upon.

Ancestors (MRO)

This view inherits methods and attributes from the following views:

Method Flowchart

  1. dispatch()
  2. http_method_not_allowed()
  3. get_template_names()
  4. get_slug_field()
  5. get_queryset()
  6. get_object()
  7. get_context_object_name()
  8. get_context_data()
  9. get()
  10. render_to_response()

Example myapp/views.py:

1
2
3
4
5
6
7
8
9
10
11
12
13
from django.views.generic.detail import DetailView
from django.utils import timezone
 
from articles.models import Article
 
class ArticleDetailView(DetailView):
 
    model = Article
 
    def get_context_data(self, **kwargs):
        context = super(ArticleDetailView, self).get_context_data(**kwargs)
        context['now'] = timezone.now()
        return context

Example myapp/urls.py:

1
2
3
4
5
6
7
from django.conf.urls import url
 
from article.views import ArticleDetailView
 
urlpatterns = [
    url(r'^(?P<slug>[-\w]+)/$', ArticleDetailView.as_view(), name='article-detail'),
]

Example myapp/article_detail.html:

1
2
3
4
5
<h1>{{ object.headline }}</h1>
<p>{{ object.content }}</p>
<p>Reporter: {{ object.reporter }}</p>
<p>Published: {{ object.pub_date|date }}</p>
<p>Date: {{ now|date }}</p>
doc_Django
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.