Generic display viewsLink to this heading
The two following generic class-based views are designed to display data. On many projects they are typically the most commonly used views.
DetailViewLink to this heading
- class django.views.generic.detail.DetailViewLink to this definition
While this view is executing,
self.objectwill contain the object that the view is operating upon.Ancestors (MRO)
This view inherits methods and attributes from the following views:
django.views.generic.detail.SingleObjectTemplateResponseMixindjango.views.generic.detail.BaseDetailView
Method Flowchart
get()
Contoh myapp/views.py:
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 contextContoh myapp/urls.py:
from django.conf.urls import url from article.views import ArticleDetailView urlpatterns = [ url(r'^(?P<slug>[-\w]+)/$', ArticleDetailView.as_view(), name='article-detail'), ]Contoh myapp/article_detail.html:
<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>
ListViewLink to this heading
- class django.views.generic.list.ListViewLink to this definition
A page representing a list of objects.
While this view is executing,
self.object_listwill 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
get()
Contoh 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 contextContoh myapp/urls.py:
from django.conf.urls import url from article.views import ArticleListView urlpatterns = [ url(r'^$', ArticleListView.as_view(), name='article-list'), ]Contoh myapp/article_list.html:
<h1>Articles</h1> <ul> {% for article in object_list %} <li>{{ article.pub_date|date }} - {{ article.headline }}</li> {% empty %} <li>No articles yet.</li> {% endfor %} </ul>
- class django.views.generic.list.BaseListViewLink to this definition
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
django.views.generic.list.ListViewor other views representing lists of objects.Ancestors (MRO)
This view inherits methods and attributes from the following views:
Cara
- get(request, *args, **kwargs)Link to this definition
Adds
object_listto the context. Ifallow_emptyis True then display an empty list. Ifallow_emptyis False then raise a 404 error.