Web 开发是一项无聊而且单调的工作。到目前为止,我们已经介绍了 Django怎样在模型和模板的层面上减小开发的单调性,但是 Web开发在视图的层面上,也经历着这种令人厌倦的事情。
例如,下面是一个呈现静态“关于”页面的URLconf:
from django.conf.urls.defaults import * from django.views.generic.simple import direct_to_template urlpatterns = patterns('', ('^about/$', direct_to_template, { 'template': 'about.html' }) )
因为通用视图都是标准的视图函数,我们可以在我们自己的视图中重用它。例如,我们扩展 about例子把映射的URL从/about/<whatever>/到一个静态渲染about/<whatever>.html。我们首先修改URL配置到新的视图函数:
from django.conf.urls.defaults import * from django.views.generic.simple import direct_to_template **from mysite.books.views import about_pages** urlpatterns = patterns('', ('^about/$', direct_to_template, { 'template': 'about.html' }), **('^about/(w+)/$', about_pages),** )
接下来,我们编写about_pages视图的代码:
from django.http import Http404 from django.template import TemplateDoesNotExist from django.views.generic.simple import direct_to_template def about_pages(request, page): try: return direct_to_template(request, template="about/%s.html" % page) except TemplateDoesNotExist: raise Http404()
让我们先看看其中的一个通用视图:对象列表视图。我们使用第五章中的Publisher来举例:
class Publisher(models.Model): name = models.CharField(maxlength=30) address = models.CharField(maxlength=50) city = models.CharField(maxlength=60) state_province = models.CharField(maxlength=30) country = models.CharField(maxlength=50) website = models.URLField() def __str__(self): return self.name class Meta: ordering = ["-name"] class Admin: pass
要为所有的书籍创建一个列表页面,我们使用下面的URL配置:
from django.conf.urls.defaults import * from django.views.generic import list_detail from mysite.books.models import Publisher publisher_info = { "queryset" : Publisher.objects.all(), } urlpatterns = patterns('', (r'^publishers/$', list_detail.object_list, publisher_info) )
这个模板将按照 context
中包含的变量object_list来渲染,这个变量包含所有的书籍对象。一个非常简单的模板看起来象下面这样:
{% extends "base.html" %} {% block content %} <h2>Publishers</h2> <ul> {% for publisher in object_list %} <li>{{ publisher.name }}</li> {% endfor %} </ul> {% endblock %}
我们可以很容易的象下面这样修改template_object_name参数的名称:
publisher_info = { "queryset" : Publisher.objects.all(), **"template_object_name" : "publisher",** } urlpatterns = patterns('', (r'^publishers/$', list_detail.object_list, publisher_info) )
这是解决方法:所有的通用视图都有一个额外的可选参数extra_context。这个参数是一个字典数据类型,包含要添加到模板的context中的额外的对象。所以要提供所有的出版商明细给视图,我们就用这样的info字典:
publisher_info = { "queryset" : Publisher.objects.all(), "template_object_name" : "publisher", **"extra_context" : {"book_list" : Book.objects.all()}** }
解决这个问题的办法是在extra_context中用一个回调(callback)来代替使用一个变量。任何可以调用的对象(例如一个函数)在传递给extra_context后都会在每次视图渲染前执行(而不是只执行一次)。你可以象这样定义一个函数:
def get_books(): return Book.objects.all() publisher_info = { "queryset" : Publisher.objects.all(), "template_object_name" : "publisher", "extra_context" : **{"book_list" : get_books}** }
或者你可以使用另一个不是那么清晰但是很简短的方法,事实上Publisher.objects.all本身就是可以调用的:
publisher_info = { "queryset" : Publisher.objects.all(), "template_object_name" : "publisher", "extra_context" : **{"book_list" : Book.objects.all}** }
举一个简单的例子,我们打算对书籍列表按出版日期排序,最近的排在最前:
book_info = { "queryset" : Book.objects.all().order_by("-publication_date"), } urlpatterns = patterns('', (r'^publishers/$', list_detail.object_list, publisher_info), **(r'^books/$', list_detail.object_list, book_info),** )
这是一个相当简单的例子,但是很说明问题。当然,你通常还想做比重新排序更多的事。如果你想要呈现某个特定出版商出版的所有书籍列表,你可以使用同样的技术:
**apress_books = {** **"queryset": Book.objects.filter(publisher__name="Apress Publishing"),** **"template_name" : "books/apress_list.html"** **}** urlpatterns = patterns('', (r'^publishers/$', list_detail.object_list, publisher_info), **(r'^books/apress/$', list_detail.object_list, apress_books),** )
如果你在请求/books/apres/时出现404错误,请检查以确保你的数据库中出版商中有名为Apress
Publishing的记录。通用视图有一个allow_empty参数可以用来处理这个情况,详情请看附录D。
用函数包装来处理复杂的数据过滤
另一个常见的需求是按URL里的关键字来过滤数据对象。在前面我们用在URL配置中硬编码出版商名称的方法来做这个,但是我们想要用一个视图就能显示某个出版商的所有书籍该怎么办呢?我们可以通过对object_list通用视图进行包装来避免写一大堆的手工代码。按惯例,我们先从写URL配置开始:
urlpatterns = patterns('', (r'^publishers/$', list_detail.object_list, publisher_info), **(r'^books/(w+)/$', books_by_publisher),** )
接下来,我们写books_by_publisher这个视图:(上面的代码中正则表达式有误,在 ‘w’前要加反斜杠)
from django.http import Http404 from django.views.generic import list_detail from mysite.books.models import Book, Publisher def books_by_publisher(request, name): # Look up the publisher (and raise a 404 if it can't be found). try: publisher = Publisher.objects.get(name__iexact=name) except Publisher.DoesNotExist: raise Http404 # Use the object_list view for the heavy lifting. return list_detail.object_list( request, queryset = Book.objects.filter(publisher=publisher), template_name = "books/books_by_publisher.html", template_object_name = "books", extra_context = {"publisher" : publisher} )
首先,我们需要在URL配置里设置指向到新的自定义视图:
from mysite.books.views import author_detail urlpatterns = patterns('', #... **(r'^authors/(?P<author_id>d+)/$', author_detail),** )
接下来写包装函数:
import datetime from mysite.books.models import Author from django.views.generic import list_detail from django.shortcuts import get_object_or_404 def author_detail(request, author_id): # Look up the Author (and raise a 404 if she's not found) author = get_object_or_404(Author, pk=author_id) # Record the last accessed date author.last_accessed = datetime.datetime.now() author.save() # Show the detail page return list_detail.object_detail( request, queryset = Author.objects.all(), object_id = author_id, )
我们可以用同样的方法修改通用视图的返回值。如果我们想要提供一个供下载用的纯文本版本的author列表,我们可以用下面这个视图:
def author_list_plaintext(request): response = list_detail.object_list( request, queryset = Author.objects.all(), mimetype = "text/plain", template_name = "books/author_list.txt" ) response["Content-Disposition"] = "attachment; filename=authors.txt" return response
这个方法之所以工作是因为通用视图返回的HttpResponse对象可以象一个字典一样的设置HTTP的头部。随便说一下,这个Content-Disposition的含义是告诉浏览器下载并保存这个页面,而不是在浏览器中显示它。
以上就是Django通用视图讲解的内容,更多相关内容请关注PHP中文网(HdhCmsTestgxlcms测试数据)!