Here again is a recurring theme of this book: at its worst, Web development is boring and monotonous. So far, weve covered how Django tries to take away some of that monotony at the model and template layers, but Web developers also experience this boredom at the view level.
Djangos generic views were developed to ease that pain. They take certain common idioms and patterns found in view development and abstract them so that you can quickly write common views of data without having to write too much code. In fact, nearly every view example in the preceding chapters could be rewritten with the help of generic views.
Chapter 8 touched briefly on how youd go about making a view generic. To review, we can recognize certain common tasks, like displaying a list of objects, and write code that displays a list of any object. Then the model in question can be passed as an extra argument to the URLconf.
Django ships with generic views to do the following:
Perform common simple tasks: redirect to a different page and render a given template.
Display list and detail pages for a single object. The event_list and entry_list views from Chapter 8 are examples of list views. A single event page is an example of what we call a detail view.
Present date-based objects in year/month/day archive pages, associated detail, and latest pages. The Django Weblogs (http://www.djangoproject.com/weblog/) year, month, and day archives are built with these, as would be a typical newspapers archives.
Allow users to create, update, and delete objects with or without authorization.
Taken together, these views provide easy interfaces to perform the most common tasks developers encounter.
All of these views are used by creating configuration dictionaries in your URLconf files and passing those dictionaries as the third member of the URLconf tuple for a given pattern.
For example, heres a simple URLconf you could use to present a static about page:
from django.conf.urls.defaults import * from django.views.generic.simple import direct_to_template urlpatterns = patterns('', ('^about/$', direct_to_template, { 'template': 'about.html' }) )
Though this might seem a bit magical at first glance look, a view with no code! , its actually exactly the same as the examples in Chapter 8: the direct_to_template view simply grabs information from the extra-parameters dictionary and uses that information when rendering the view.
Because this generic view and all the others is a regular view functions like any other, we can reuse it inside our own views. As an example, lets extend our about example to map URLs of the form /about/<whatever>/ to statically rendered about/<whatever>.html . Well do this by first modifying the URLconf to point to a view function:
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),** )
Next, well write the about_pages view:
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()
Here were treating direct_to_template like any other function. Since it returns an HttpResponse , we can simply return it as-is. The only slightly tricky business here is dealing with missing templates. We dont want a nonexistent template to cause a server error, so we catch TemplateDoesNotExist exceptions and return 404 errors instead.
Is There a Security Vulnerability Here?
Sharp-eyed readers may have noticed a possible security hole: were constructing the template name using interpolated content from the browser (template="about/%s.html" % page ). At first glance, this looks like a classic directory traversal vulnerability (discussed in detail in Chapter 19). But is it really?
Not exactly. Yes, a maliciously crafted value of page could cause directory traversal, but although page is taken from the request URL, not every value will be accepted. They key is in the URLconf: were using the regular expression \w+ to match the page part of the URL, and \w only accepts letters and numbers. Thus, any malicious characters (dots and slashes, here) will be rejected by the URL resolver before they reach the view itself.
The direct_to_template certainly is useful, but Djangos generic views really shine when it comes to presenting views on your database content. Because its such a common task, Django comes with a handful of built-in generic views that make generating list and detail views of objects incredibly easy.
Lets take a look at one of these generic views: the object list view. Well be using this Publisher object from Chapter 5:
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
To build a list page of all books, wed use a URLconf along these lines:
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) )
Thats all the Python code we need to write. We still need to write a template, however. We could explicitly tell the object_list view which template to use by including a template_name key in the extra arguments dictionary, but in the absence of an explicit template Django will infer one from the objects name. In this case, the inferred template will be "books/publisher_list.html" the books part comes from the name of the app that defines the model, while the publisher bit is just the lowercased version of the models name.
This template will be rendered against a context containing a variable called object_list that contains all the book objects. A very simple template might look like the following:
{% extends "base.html" %} {% block content %} <h2>Publishers</h2> <ul> {% for publisher in object_list %} <li>{{ publisher.name }}</li> {% endfor %} </ul> {% endblock %}
Thats really all there is to it. All the cool features of generic views come from changing the info dictionary passed to the generic view. Appendix D documents all the generic views and all their options in detail; the rest of this chapter will consider some of the common ways you might customize and extend generic views.
Theres no question that using generic views can speed up development substantially. In most projects, however, there comes a moment when the generic views no longer suffice. Indeed, the most common question asked by new Django developers is how to make generic views handle a wider array of situations.
Luckily, in nearly every one of these cases, there are ways to simply extend generic views to handle a larger array of use cases. These situations usually fall into a handful of patterns dealt with in the sections that follow.
You might have noticed that sample publisher list template stores all the books in a variable named object_list . While this works just fine, it isnt all that friendly to template authors: they have to just know that theyre dealing with books here. A better name for that variable would be publisher_list ; that variables content is pretty obvious.
We can change the name of that variable easily with the template_object_name argument:
publisher_info = { "queryset" : Publisher.objects.all(), **"template_object_name" : "publisher",** } urlpatterns = patterns('', (r'^publishers/$', list_detail.object_list, publisher_info) )
Providing a useful template_object_name is always a good idea. Your coworkers who design templates will thank you.
Often you simply need to present some extra information beyond that provided by the generic view. For example, think of showing a list of all the other publishers on each publisher detail page. The object_detail generic view provides the publisher to the context, but it seems theres no way to get a list of all publishers in that template.
But there is: all generic views take an extra optional parameter, extra_context . This is a dictionary of extra objects that will be added to the templates context. So, to provide the list of all publishers on the detail detail view, wed use an info dict like this:
publisher_info = { "queryset" : Publisher.objects.all(), "template_object_name" : "publisher", **"extra_context" : {"book_list" : Book.objects.all()}** }
This would populate a {{ book_list }} variable in the template context. This pattern can be used to pass any information down into the template for the generic view. Its very handy.
However, theres actually a subtle bug here can you spot it?
The problem has to do with when the queries in extra_context are evaluated. Because this example puts Publisher.objects.all() in the URLconf, it will be evaluated only once (when the URLconf is first loaded). Once you add or remove publishers, youll notice that the generic view doesnt reflect those changes until you reload the Web server (see Caching and QuerySets in Appendix C for more information about when QuerySets are cached and evaluated).
Note
This problem doesnt apply to the queryset generic view argument. Since Django knows that particular QuerySet should never be cached, the generic view takes care of clearing the cache when each view is rendered.
The solution is to use a callback in extra_context instead of a value. Any callable (i.e., a function) thats passed to extra_context will be evaluated when the view is rendered (instead of only once). You could do this with an explicitly defined function:
def get_books(): return Book.objects.all() publisher_info = { "queryset" : Publisher.objects.all(), "template_object_name" : "publisher", "extra_context" : **{"book_list" : get_books}** }
or you could use a less obvious but shorter version that relies on the fact that Publisher.objects.all is itself a callable:
publisher_info = { "queryset" : Publisher.objects.all(), "template_object_name" : "publisher", "extra_context" : **{"book_list" : Book.objects.all}** }
Notice the lack of parentheses after Book.objects.all ; this references the function without actually calling it (which the generic view will do later).
Now lets take a closer look at this queryset key weve been using all along. Most generic views take one of these queryset arguments its how the view knows which set of objects to display (see Selecting Objects in Chapter 5 for an introduction to QuerySets, and see Appendix C for the complete details).
To pick a simple example, we might want to order a list of books by publication date, with the most recent first:
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),** )
Thats a pretty simple example, but it illustrates the idea nicely. Of course, youll usually want to do more than just reorder objects. If you want to present a list of books by a particular publisher, you can use the same technique:
**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),** )
Notice that along with a filtered queryset , were also using a custom template name. If we didnt, the generic view would use the same template as the vanilla object list, which might not be what we want.
Also notice that this isnt a very elegant way of doing publisher-specific books. If we want to add another publisher page, wed need another handful of lines in the URLconf, and more than a few publishers would get unreasonable. Well deal with this problem in the next section.
Note
If you get a 404 when requesting /books/apress/ , check to ensure you actually have a Publisher with the name Apress Publishing. Generic views have an allow_empty parameter for this case. See Appendix D for more details.
Another common need is to filter down the objects given in a list page by some key in the URL. Earlier we hard-coded the publishers name in the URLconf, but what if we wanted to write a view that displayed all the books by some arbitrary publisher? We can wrap the object_list generic view to avoid writing a lot of code by hand. As usual, well start by writing a URLconf:
urlpatterns = patterns('', (r'^publishers/$', list_detail.object_list, publisher_info), **(r'^books/(w+)/$', books_by_publisher),** )
Next, well write the books_by_publisher view itself:
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} )
This works because theres really nothing special about generic views theyre just Python functions. Like any view function, generic views expect a certain set of arguments and return HttpResponse objects. Thus, its incredibly easy to wrap a small function around a generic view that does additional work before (or after; see the next section) handing things off to the generic view.
Note
Notice that in the preceding example we passed the current publisher being displayed in the extra_context . This is usually a good idea in wrappers of this nature; it lets the template know which parent object is currently being browsed.
The last common pattern well look at involves doing some extra work before or after calling the generic view.
Imagine we had a last_accessed field on our Author object that we were using to keep track of the last time anybody looked at that author. The generic object_detail view, of course, wouldnt know anything about this field, but once again we could easily write a custom view to keep that field updated.
First, wed need to add an author detail bit in the URLconf to point to a custom view:
from mysite.books.views import author_detail urlpatterns = patterns('', #... **(r'^authors/(?P<author_id>d+)/$', author_detail),** )
Then wed write our wrapper function:
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, )
Note
This code wont actually work unless you add a last_accessed field to your Author model and create a books/author_detail.html template.
We can use a similar idiom to alter the response returned by the generic view. If we wanted to provide a downloadable plain-text version of the list of authors, we could use a view like this:
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
This works because the generic views return simple HttpResponse objects that can be treated like dictionaries to set HTTP headers. This Content-Disposition business, by the way, instructs the browser to download and save the page instead of displaying it in the browser.
In this chapter we looked at only a couple of the generic views Django ships with, but the general ideas presented here should apply pretty closely to any generic view. Appendix D covers all the available views in detail, and its recommended reading if you want to get the most out of this powerful feature.
In the next chapter we delve deep into the inner workings of Djangos templates, showing all the cool ways they can be extended. Until now, weve treated the template engine as a mostly static tool you can use to render your content.
鍏充簬鏈瘎娉ㄧ郴缁
鏈珯浣跨敤涓婁笅鏂囧叧鑱旂殑璇勬敞绯荤粺鏉ユ敹闆嗗弽棣堜俊鎭備笉鍚屼簬涓鑸鏁寸珷鍋氳瘎娉ㄧ殑鍋氭硶锛 鎴戜滑鍏佽浣犲姣忎竴涓嫭绔嬬殑鈥滄枃鏈潡鈥濆仛璇勬敞銆備竴涓滄枃鏈潡鈥濈湅璧锋潵鏄繖鏍风殑锛
涓涓滄枃鏈潡鈥濇槸涓涓钀斤紝涓涓垪琛ㄩ」锛屼竴娈典唬鐮侊紝鎴栬呭叾浠栦竴灏忔鍐呭銆 浣犻変腑瀹冧細楂樹寒搴︽樉绀:
瑕佸鏂囨湰鍧楀仛璇勬敞锛屼綘鍙渶瑕佺偣鍑诲畠鏃佽竟鐨勬爣璇嗗潡:
鎴戜滑浼氫粩缁嗛槄璇绘瘡涓瘎璁猴紝濡傛灉鍙兘鐨勮瘽鎴戜滑涔熶細鎶婅瘎娉ㄨ冭檻鍒版湭鏉ョ殑鐗堟湰涓幓:
濡傛灉浣犳効鎰忎綘鐨勮瘎娉ㄨ閲囩敤锛岃纭繚鐣欎笅浣犵殑鍏ㄥ悕 (娉ㄦ剰涓嶆槸鏄电О鎴栫畝绉帮級
Many, many thanks to Jack Slocum; the inspiration and much of the code for the comment system comes from Jack's blog, and this site couldn't have been built without his wonderful
YAHOO.ext
library. Thanks also to Yahoo for YUI itself.