01-介绍
通用视图把视图开发中常用的写法和模式抽象出来,让你编写少量代码就能快速实现常见的数据视图。显示对象列表就是这样一种任务。
Django 自带的通用视图能实现下述功能: 1、列出对象并显示单个对象的详细信息。如果创建一个管理会议的应用程序,那么 TalkListView 和 Reg- isteredUserListView 就是列表视图。某一个演讲的页面就是详细信息视图。 2、呈现基于日期的对象,显示为年月日归档页面(带有详细信息),以及“最新”页面。 3、让用户创建、更新和删除对象——需不需要授权都行。
# models.py from django.db import models class Publisher(models.Model): name = models.CharField(max_length=30) address = models.CharField(max_length=50) city = models.CharField(max_length=60) state_province = models.CharField(max_length=30) country = models.CharField(max_length=50) website = models.URLField() class Meta: ordering = ["-name"] def __str__(self): return self.name class Author(models.Model): salutation = models.CharField(max_length=10) name = models.CharField(max_length=200) email = models.EmailField() headshot = models.ImageField(upload_to='author_headshots') def __str__(self): return self.name class Book(models.Model): title = models.CharField(max_length=100) authors = models.ManyToManyField('Author') publisher = models.ForeignKey(Publisher) publication_date = models.DateField()
02-对象的通用视图-ListView
# views.py from django.views.generic import ListView from books.models import Publisher class PublisherList(ListView): model = Publisher # 指定对应的模板 template_name = books/ publisher_list.html # urls.py from django.conf.urls import url from books.views import PublisherList urlpatterns = [ url(r'^publishers/$', PublisherList.as_view()), ]
# 渲染这个模板时,上下文中有个名为 object_list 的变量,它的值是所有出版社对象。 {% extends "base.html" %} {% block content %} <h2>Publishers</h2> <ul> {% for publisher in object_list %} <li>{{ publisher.name }}</li> {% endfor %} </ul> {% endblock %}
03-指定模板上下文的名称 - context_object_name
# 默认的为 object_name class PublisherList(ListView): model = Publisher context_object_name = 'my_favorite_publishers'
04-提供额外的上下文变量 - get_context_data
通常,除了通用视图提供的信息之外,还想显示一些额外信息。但是如何在模板中获取额外的信息呢?
答案是扩展 ListView、DetailView等,自己实现 get_context_data 方法。
默认的实现只为模板提供该显示的对象,不过可 以覆盖,提供更多信息:
from django.views.generic import DetailView from books.models import Publisher, Book class PublisherDetail(DetailView): model = Publisher def get_context_data(self, **kwargs): # 先调用原来的实现,获取上下文 context = super(PublisherDetail, self).get_context_data(**kwargs) # 把所有图书构成的查询集合添加到上下文中 context['book_list'] = Book.objects.all() return context
提示:默认情况下,get_context_data 会把所有父类的上下文数据与当前类的合并在一起。如果在调 整上下文的子类中不想使用这种行为,要在超类上调用 get_context_data。
如果两个类没有在 上下文中定义相同的键,这样得到的结果符合预期。
但是,如果尝试覆盖超类设定的键(在调 用 super 之后),当子类想覆盖所有超类时,子类也必须在调用 super 之后显式设定那个键。
05-显示对象的子集 - queryset
from django.views.generic import DetailView from books.models import Publisher class PublisherDetail(DetailView): context_object_name = 'publisher' # model = Publisher 其实是 queryset = Publisher.objects.all() 的简洁形式。 # 然而,使用 queryset 可以过滤 对象列表,进一步指定要在视图中查看的对象。如:过滤等 queryset = Publisher.objects.all()
from django.views.generic import ListView from books.models import Book class BookList(ListView): queryset = Book.objects.order_by('-publication_date') context_object_name = 'book_list'
06-动态过滤 - get_queryset()
需求:根据 URL 中指定的键过滤列表页面中的对象。 解决方法:覆盖 ListView 的 get_queryset() 方法。它的默认实现是返回 queryset 属性的值,不过可以添加更多的逻辑。调用基于类的视图时,很多有用的东西存储到 self 中了,除了请 求(self.request)之外,还有根据 URL 配置捕获的位置参数(self.args)和关键字参数 (self.kwargs)。 # urls.py from django.conf.urls import url from books.views import PublisherBookList urlpatterns = [ url(r'^books/([w-]+)/$', PublisherBookList.as_view()), ] # views.py from django.shortcuts import get_object_or_404 from django.views.generic import ListView from books.models import Book, Publisher class PublisherBookList(ListView): template_name = 'books/books_by_publisher.html' def get_queryset(self): self.publisher = get_object_or_404(Publisher, name=self.args[0]) return Book.objects.filter(publisher=self.publisher)
如果需要,可以使用 self.request.user 通过当前用户过 滤,或者实现其他更复杂的逻辑。与此同时,我们还可以把出版社对象添加到上下文中,供模板使用:
def get_context_data(self, **kwargs): # 先调用原来的实现,获取上下文 context = super(PublisherBookList, self).get_context_data(**kwargs) # 添加出版社对象 context['publisher'] = self.publisher return context ## 执行额外的操作
07-在调用通用视图前后做些额外工作 - get_object()
假设 Author 模型中有个 last_accessed 字 段,用于记录这位作者的信息被人查看的最后时间:
# models.py from django.db import models class Author(models.Model): salutation = models.CharField(max_length=10) name = models.CharField(max_length=200) email = models.EmailField() headshot = models.ImageField(upload_to='author_headshots') last_accessed = models.DateTimeField() # urls.py from django.conf.urls import url from books.views import AuthorDetailView urlpatterns = [ #... url(r'^authors/(?P<pk>[0-9]+)/$', AuthorDetailView.as_view(), name='author-detail'), ] # views.py from django.views.generic import DetailView from django.utils import timezone from books.models import Author class AuthorDetailView(DetailView): queryset = Author.objects.all() def get_object(self): # 调用超类中的同名方法 object = super(AuthorDetailView, self).get_object() # 记录最后访问日期 object.last_accessed = timezone.now() object.save() # 返回对象 return object # 注意:这里,URL 配置使用的分组名为 pk,这是 DetailView 过滤查询集合时查找主键所用的默认名称。 # 如果把这个分组命名为其他值,要在视图中设定 pk_url_kwarg。详情参见 DetailView 的文档。