Generic display views

    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. get_template_names()
    3. get_queryset()
    4. get_context_object_name()
    5. get()

    Example myapp/views.py:

    Example myapp/urls.py:

    1. from django.urls import path
    2. from article.views import ArticleDetailView
    3. urlpatterns = [
    4. path('<slug:slug>/', ArticleDetailView.as_view(), name='article-detail'),
    5. ]

    Example myapp/article_detail.html:

    class django.views.generic.detail.BaseDetailView

    A base view for displaying a single object. It is not intended to be used directly, but rather as a parent class of the django.views.generic.detail.DetailView or other views representing details of a single object.

    This view inherits methods and attributes from the following views:

    Methods

    • get(request, \args, **kwargs*)

      Adds to the context.

    ListView

    class django.views.generic.list.ListView

    A page representing a list of objects.

    While this view is executing, self.object_list will contain the list of objects (usually, but not necessarily a queryset) that the view is operating upon.

    Ancestors (MRO)

    This view inherits methods and attributes from the following views:

    Method Flowchart

    1. dispatch()
    2. get_template_names()
    3. get_context_object_name()
    4. get()
    1. from django.utils import timezone
    2. from articles.models import Article
    3. class ArticleListView(ListView):
    4. model = Article
    5. paginate_by = 100 # if pagination is desired
    6. context = super().get_context_data(**kwargs)
    7. context['now'] = timezone.now()
    8. return context

    Example myapp/urls.py:

    Example myapp/article_list.html:

    1. <h1>Articles</h1>
    2. <ul>
    3. {% for article in object_list %}
    4. <li>{{ article.pub_date|date }} - {{ article.headline }}</li>
    5. {% empty %}
    6. <li>No articles yet.</li>
    7. {% endfor %}
    8. </ul>

    If you’re using pagination, you can adapt the example template from the pagination docs.

    class django.views.generic.list.BaseListView

    A base view for displaying a list of objects. It is not intended to be used directly, but rather as a parent class of the or other views representing lists of objects.

    Ancestors (MRO)

    This view inherits methods and attributes from the following views:

    Methods

    • get(request, \args, **kwargs*)

      Adds to the context. If is True then display an empty list. If allow_empty is False then raise a 404 error.