Views de exibição genéricas

As duas “class-based views” genéricas a seguir são utilizadas para exibir dados. Em vários projetos elas são as views mais comumente usadas.

DetailView

class django.views.generic.detail.DetailView

Enquanto esta view é executada, self.object conterá o objeto que a view está operando em cima.

Ancestrais (MRO)

Essa “view” herda métodos e atributos das seguintes “views”:

Fluxograma de métodos

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

Exemplo myapp/views.py:

from django.utils import timezone
from django.views.generic.detail import DetailView

from articles.models import Article


class ArticleDetailView(DetailView):
    model = Article

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

Exemplo myapp/urls.py:

from django.urls import path

from article.views import ArticleDetailView

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

Exemplo 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>
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.

Ancestrais (MRO)

Essa “view” herda métodos e atributos das seguintes “views”:

Métodos

get(request, *args, **kwargs)

Adds object to the context.

ListView

class django.views.generic.list.ListView

Uma página representando uma lista de objetos

Enquanto esta view é executada, self.object_list conterá a lista de objetos (geralmente, mas não necessariamente uma queryset) que a view está operando em cima.

Ancestrais (MRO)

Essa “view” herda métodos e atributos das seguintes “views”:

Fluxograma de métodos

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

Examplo views.py:

from django.utils import timezone
from django.views.generic.list import ListView

from articles.models import Article


class ArticleListView(ListView):
    model = Article
    paginate_by = 100  # if pagination is desired

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

Exemplo myapp/urls.py:

from django.urls import path

from article.views import ArticleListView

urlpatterns = [
    path("", ArticleListView.as_view(), name="article-list"),
]

Exemplo 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>

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

class django.views.generic.list.BaseListView

Uma view base para exibição de uma lista de objetos. Seu objetivo não é ser usada diretamente, mas como classe pai de django.views.generic.list.ListView ou outras views representando listas de objetos.

Ancestrais (MRO)

Essa “view” herda métodos e atributos das seguintes “views”:

Métodos

get(request, *args, **kwargs)

Adiciona object_list no contexto. Se allow_empty for True então uma lista vazia é exbida. Se allow_empty for False então será gerado um erro 404.

Back to Top