List/detail generic views

The list/detail generic views handle the common case of displaying a list of items at one view and individual detail views of those items at another.

Lists of objects

django.views.generic.list.ListView 

Use this view to display a page representing a list of objects.

Example views.py:

from django.views.generic.list import ListView 
from django.utils import timezone 
 
from articles.models import Article 
 
class ArticleListView(ListView): 
 
    model = Article 
 
    def get_context_data(self, **kwargs): 
        context = super(ArticleListView, self).get_context_data(**kwargs) 
        context['now'] = timezone.now() 
        return context 

Example myapp/urls.py:

from django.conf.urls import url from article.views import ArticleListView urlpatterns = [ url(r'^$', ArticleListView.as_view(), ...

Get Mastering Django: Core now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.