Ticket #10336: 10366-generic-views-topics-docs-r10365.diff

File 10366-generic-views-topics-docs-r10365.diff, 24.8 KB (added by Ramiro Morales, 10 years ago)

Patch updated to post-r10281

  • docs/index.txt

    diff --git a/docs/index.txt b/docs/index.txt
    a b  
    9898      :ref:`Storage API <ref-files-storage>` |
    9999      :ref:`Managing files <topics-files>` |
    100100      :ref:`Custom storage <howto-custom-file-storage>`
    101          
    102     * **Advanced:**
    103       :ref:`Generic views <ref-generic-views>` |
    104       :ref:`Generating CSV <howto-outputting-csv>` |
     101
     102    * **Generic views:**
     103      :ref:`Overview<topics-generic-views>` |
     104      :ref:`Built-in generic views<ref-generic-views>`
     105
     106    * **Advanced:**
     107      :ref:`Generating CSV <howto-outputting-csv>` |
    105108      :ref:`Generating PDF <howto-outputting-pdf>`
    106109   
    107110    * **Middleware:**
  • docs/internals/documentation.txt

    diff --git a/docs/internals/documentation.txt b/docs/internals/documentation.txt
    a b  
    130130
    131131The work is mostly done, but here's what's left, in rough order of priority.
    132132
    133     * Fix up generic view docs: adapt Chapter 9 of the Django Book (consider
    134       this TODO item my permission and license) into
    135       ``topics/generic-views.txt``; remove the intro material from
    136       ``ref/generic-views.txt`` and just leave the function reference.
    137 
    138133    * Change the "Added/changed in development version" callouts to proper
    139134      Sphinx ``.. versionadded::`` or ``.. versionchanged::`` directives.
    140135
  • docs/ref/generic-views.txt

    diff --git a/docs/ref/generic-views.txt b/docs/ref/generic-views.txt
    a b  
    44Generic views
    55=============
    66
    7 Writing Web applications can be monotonous, because we repeat certain patterns
    8 again and again. In Django, the most common of these patterns have been
    9 abstracted into "generic views" that let you quickly provide common views of
    10 an object without actually needing to write any Python code.
    11 
    12 Django's generic views contain the following:
    13 
    14     * A set of views for doing list/detail interfaces.
    15 
    16     * A set of views for year/month/day archive pages and associated
    17       detail and "latest" pages (for example, the Django weblog's year_,
    18       month_, day_, detail_, and latest_ pages).
    19 
    20     * A set of views for creating, editing, and deleting objects.
    21 
    22 .. _year: http://www.djangoproject.com/weblog/2005/
    23 .. _month: http://www.djangoproject.com/weblog/2005/jul/
    24 .. _day: http://www.djangoproject.com/weblog/2005/jul/20/
    25 .. _detail: http://www.djangoproject.com/weblog/2005/jul/20/autoreload/
    26 .. _latest: http://www.djangoproject.com/weblog/
    27 
    28 All of these views are used by creating configuration dictionaries in
    29 your URLconf files and passing those dictionaries as the third member of the
    30 URLconf tuple for a given pattern. For example, here's the URLconf for the
    31 simple weblog app that drives the blog on djangoproject.com::
    32 
    33     from django.conf.urls.defaults import *
    34     from django_website.apps.blog.models import Entry
    35 
    36     info_dict = {
    37         'queryset': Entry.objects.all(),
    38         'date_field': 'pub_date',
    39     }
    40 
    41     urlpatterns = patterns('django.views.generic.date_based',
    42        (r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/(?P<slug>[-\w]+)/$', 'object_detail', info_dict),
    43        (r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/$',               'archive_day',   info_dict),
    44        (r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$',                                'archive_month', info_dict),
    45        (r'^(?P<year>\d{4})/$',                                                    'archive_year',  info_dict),
    46        (r'^$',                                                                    'archive_index', info_dict),
    47     )
    48 
    49 As you can see, this URLconf defines a few options in ``info_dict``.
    50 ``'queryset'`` gives the generic view a ``QuerySet`` of objects to use (in this
    51 case, all of the ``Entry`` objects) and tells the generic view which model is
    52 being used.
    53 
    547Documentation of each generic view follows, along with a list of all keyword
    55 arguments that a generic view expects. Remember that as in the example above,
    56 arguments may either come from the URL pattern (as ``month``, ``day``,
    57 ``year``, etc. do above) or from the additional-information dictionary (as for
    58 ``queryset``, ``date_field``, etc.).
     8arguments that a generic view expects. Remember that arguments may either come
     9from the URL pattern or from the ``extra_context`` additional-information
     10dictionary.
    5911
    6012Most generic views require the ``queryset`` key, which is a ``QuerySet``
    6113instance; see :ref:`topics-db-queries` for more information about ``QuerySet``
    6214objects.
    6315
    64 Most views also take an optional ``extra_context`` dictionary that you can use
    65 to pass any auxiliary information you wish to the view. The values in the
    66 ``extra_context`` dictionary can be either functions (or other callables) or
    67 other objects. Functions are evaluated just before they are passed to the
    68 template. However, note that QuerySets retrieve and cache their data when they
    69 are first evaluated, so if you want to pass in a QuerySet via
    70 ``extra_context`` that is always fresh you need to wrap it in a function or
    71 lambda that returns the QuerySet.
    72 
    7316"Simple" generic views
    7417======================
    7518
  • new file docs/topics/generic-views.txt

    diff --git a/docs/topics/generic-views.txt b/docs/topics/generic-views.txt
    new file mode 100644
    - +  
     1.. _topics-generic-views:
     2
     3=============
     4Generic views
     5=============
     6
     7Writing Web applications can be monotonous, because we repeat certain patterns
     8again and again. Django tries to take away some of that monotony at the model
     9and template layers, but Web developers also experience this boredom at the view
     10level.
     11
     12Django's *generic views* were developed to ease that pain. They take certain
     13common idioms and patterns found in view development and abstract them so that
     14you can quickly write common views of data without having to write too much
     15code.
     16
     17We can recognize certain common tasks, like displaying a list of objects, and
     18write code that displays a list of *any* object. Then the model in question can
     19be passed as an extra argument to the URLconf.
     20
     21Django ships with generic views to do the following:
     22
     23    * Perform common "simple" tasks: redirect to a different page and
     24      render a given template.
     25
     26    * Display list and detail pages for a single object. If we were creating an
     27      application to manage conferences then a ``talk_list`` view and a
     28      ``registered_user_list`` view would be examples of list views. A single
     29      talk page is an example of what we call a "detail" view.
     30
     31    * Present date-based objects in year/month/day archive pages,
     32      associated detail, and "latest" pages. The Django Weblog's
     33      (http://www.djangoproject.com/weblog/) year, month, and
     34      day archives are built with these, as would be a typical
     35      newspaper's archives.
     36
     37    * Allow users to create, update, and delete objects -- with or
     38      without authorization.
     39
     40Taken together, these views provide easy interfaces to perform the most common
     41tasks developers encounter.
     42
     43Using generic views
     44===================
     45
     46All of these views are used by creating configuration dictionaries in
     47your URLconf files and passing those dictionaries as the third member of the
     48URLconf tuple for a given pattern.
     49
     50For example, here's a simple URLconf you could use to present a static "about"
     51page::
     52
     53    from django.conf.urls.defaults import *
     54    from django.views.generic.simple import direct_to_template
     55
     56    urlpatterns = patterns('',
     57        ('^about/$', direct_to_template, {
     58            'template': 'about.html'
     59        })
     60    )
     61
     62Though this might seem a bit "magical" at first glance  -- look, a view with no
     63code! --, actually the ``direct_to_template`` view simply grabs information from
     64the extra-parameters dictionary and uses that information when rendering the
     65view.
     66
     67Because this generic view -- and all the others -- is a regular view functions
     68like any other, we can reuse it inside our own views. As an example, let's
     69extend our "about" example to map URLs of the form ``/about/<whatever>/`` to
     70statically rendered ``about/<whatever>.html``. We'll do this by first modifying
     71the URLconf to point to a view function:
     72
     73.. parsed-literal::
     74
     75    from django.conf.urls.defaults import *
     76    from django.views.generic.simple import direct_to_template
     77    **from mysite.books.views import about_pages**
     78
     79    urlpatterns = patterns('',
     80        ('^about/$', direct_to_template, {
     81            'template': 'about.html'
     82        }),
     83        **('^about/(\w+)/$', about_pages),**
     84    )
     85
     86Next, we'll write the ``about_pages`` view::
     87
     88    from django.http import Http404
     89    from django.template import TemplateDoesNotExist
     90    from django.views.generic.simple import direct_to_template
     91
     92    def about_pages(request, page):
     93        try:
     94            return direct_to_template(request, template="about/%s.html" % page)
     95        except TemplateDoesNotExist:
     96            raise Http404()
     97
     98Here we're treating ``direct_to_template`` like any other function. Since it
     99returns an ``HttpResponse``, we can simply return it as-is. The only slightly
     100tricky business here is dealing with missing templates. We don't want a
     101nonexistent template to cause a server error, so we catch
     102``TemplateDoesNotExist`` exceptions and return 404 errors instead.
     103
     104.. admonition:: Is there a security vulnerability here?
     105
     106    Sharp-eyed readers may have noticed a possible security hole: we're
     107    constructing the template name using interpolated content from the browser
     108    (``template="about/%s.html" % page``). At first glance, this looks like a
     109    classic *directory traversal* vulnerability. But is it really?
     110
     111    Not exactly. Yes, a maliciously crafted value of ``page`` could cause
     112    directory traversal, but although ``page`` *is* taken from the request URL,
     113    not every value will be accepted. The key is in the URLconf: we're using
     114    the regular expression ``\w+`` to match the ``page`` part of the URL, and
     115    ``\w`` only accepts letters and numbers. Thus, any malicious characters
     116    (dots and slashes, here) will be rejected by the URL resolver before they
     117    reach the view itself.
     118
     119Generic views of objects
     120========================
     121
     122The ``direct_to_template`` certainly is useful, but Django's generic views
     123really shine when it comes to presenting views on your database content. Because
     124it's such a common task, Django comes with a handful of built-in generic views
     125that make generating list and detail views of objects incredibly easy.
     126
     127Let's take a look at one of these generic views: the "object list" view. We'll
     128be using these models::
     129
     130    # models.py
     131    from django.db import models
     132
     133    class Publisher(models.Model):
     134        name = models.CharField(max_length=30)
     135        address = models.CharField(max_length=50)
     136        city = models.CharField(max_length=60)
     137        state_province = models.CharField(max_length=30)
     138        country = models.CharField(max_length=50)
     139        website = models.URLField()
     140
     141        def __unicode__(self):
     142            return self.name
     143
     144        class Meta:
     145            ordering = ["-name"]
     146
     147    class Book(models.Model):
     148        title = models.CharField(max_length=100)
     149        authors = models.ManyToManyField('Author')
     150        publisher = models.ForeignKey(Publisher)
     151        publication_date = models.DateField()
     152
     153To build a list page of all books, we'd use a URLconf along these lines::
     154
     155    from django.conf.urls.defaults import *
     156    from django.views.generic import list_detail
     157    from mysite.books.models import Publisher
     158
     159    publisher_info = {
     160        "queryset" : Publisher.objects.all(),
     161    }
     162
     163    urlpatterns = patterns('',
     164        (r'^publishers/$', list_detail.object_list, publisher_info)
     165    )
     166
     167That's all the Python code we need to write. We still need to write a template,
     168however. We could explicitly tell the ``object_list`` view which template to use
     169by including a ``template_name`` key in the extra arguments dictionary, but in
     170the absence of an explicit template Django will infer one from the object's
     171name. In this case, the inferred template will be
     172``"books/publisher_list.html"`` -- the "books" part comes from the name of the
     173app that defines the model, while the "publisher" bit is just the lowercased
     174version of the model's name.
     175
     176.. highlightlang:: html+django
     177
     178This template will be rendered against a context containing a variable called
     179``object_list`` that contains all the book objects. A very simple template
     180might look like the following::
     181
     182    {% extends "base.html" %}
     183
     184    {% block content %}
     185        <h2>Publishers</h2>
     186        <ul>
     187            {% for publisher in object_list %}
     188                <li>{{ publisher.name }}</li>
     189            {% endfor %}
     190        </ul>
     191    {% endblock %}
     192
     193That's really all there is to it. All the cool features of generic views come
     194from changing the "info" dictionary passed to the generic view. The
     195:ref:`generic views reference<ref-generic-views>` documents all the generic
     196views and all their options in detail; the rest of this document will consider
     197some of the common ways you might customize and extend generic views.
     198
     199Extending generic views
     200=======================
     201
     202.. highlightlang:: python
     203
     204There's no question that using generic views can speed up development
     205substantially. In most projects, however, there comes a moment when the
     206generic views no longer suffice. Indeed, the most common question asked by new
     207Django developers is how to make generic views handle a wider array of
     208situations.
     209
     210Luckily, in nearly every one of these cases, there are ways to simply extend
     211generic views to handle a larger array of use cases. These situations usually
     212fall into a handful of patterns dealt with in the sections that follow.
     213
     214Making "friendly" template contexts
     215-----------------------------------
     216
     217You might have noticed that our sample publisher list template stores all the
     218books in a variable named ``object_list``. While this works just fine, it isn't
     219all that "friendly" to template authors: they have to "just know" that they're
     220dealing with books here. A better name for that variable would be
     221``publisher_list``; that variable's content is pretty obvious.
     222
     223We can change the name of that variable easily with the ``template_object_name``
     224argument:
     225
     226.. parsed-literal::
     227
     228    publisher_info = {
     229        "queryset" : Publisher.objects.all(),
     230        **"template_object_name" : "publisher",**
     231    }
     232
     233    urlpatterns = patterns('',
     234        (r'^publishers/$', list_detail.object_list, publisher_info)
     235    )
     236
     237Providing a useful ``template_object_name`` is always a good idea. Your
     238coworkers who design templates will thank you.
     239
     240Adding extra context
     241--------------------
     242
     243Often you simply need to present some extra information beyond that provided by
     244the generic view. For example, think of showing a list of all the other
     245publishers on each publisher detail page. The ``object_detail`` generic view
     246provides the publisher to the context, but it seems there's no way to get a list
     247of *all* publishers in that template.
     248
     249But there is: all generic views take an extra optional parameter,
     250``extra_context``. This is a dictionary of extra objects that will be added to
     251the template's context. So, to provide the list of all publishers on the detail
     252detail view, we'd use an info dict like this:
     253
     254.. parsed-literal::
     255
     256    from mysite.books.models import Publisher, **Book**
     257
     258    publisher_info = {
     259        "queryset" : Publisher.objects.all(),
     260        "template_object_name" : "publisher",
     261        **"extra_context" : {"book_list" : Book.objects.all()}**
     262    }
     263
     264This would populate a ``{{ book_list }}`` variable in the template context.
     265This pattern can be used to pass any information down into the template for the
     266generic view. It's very handy.
     267
     268However, there's actually a subtle bug here -- can you spot it?
     269
     270The problem has to do with when the queries in ``extra_context`` are evaluated.
     271Because this example puts ``Publisher.objects.all()`` in the URLconf, it will
     272be evaluated only once (when the URLconf is first loaded). Once you add or
     273remove publishers, you'll notice that the generic view doesn't reflect those
     274changes until you reload the Web server (see :ref:`caching-and-querysets`
     275for more information about when QuerySets are cached and evaluated).
     276
     277.. note::
     278
     279    This problem doesn't apply to the ``queryset`` generic view argument. Since
     280    Django knows that particular QuerySet should *never* be cached, the generic
     281    view takes care of clearing the cache when each view is rendered.
     282
     283The solution is to use a callback in ``extra_context`` instead of a value. Any
     284callable (i.e., a function) that's passed to ``extra_context`` will be evaluated
     285when the view is rendered (instead of only once). You could do this with an
     286explicitly defined function:
     287
     288.. parsed-literal::
     289
     290    def get_books():
     291        return Book.objects.all()
     292
     293    publisher_info = {
     294        "queryset" : Publisher.objects.all(),
     295        "template_object_name" : "publisher",
     296        "extra_context" : **{"book_list" : get_books}**
     297    }
     298
     299or you could use a less obvious but shorter version that relies on the fact that
     300``Publisher.objects.all`` is itself a callable:
     301
     302.. parsed-literal::
     303
     304    publisher_info = {
     305        "queryset" : Publisher.objects.all(),
     306        "template_object_name" : "publisher",
     307        "extra_context" : **{"book_list" : Book.objects.all}**
     308    }
     309
     310Notice the lack of parentheses after ``Book.objects.all``; this references
     311the function without actually calling it (which the generic view will do later).
     312
     313Viewing subsets of objects
     314--------------------------
     315
     316Now let's take a closer look at this ``queryset`` key we've been using all
     317along. Most generic views take one of these ``queryset`` arguments -- it's how
     318the view knows which set of objects to display (see :ref:`topics-db-queries` for
     319more information about ``QuerySet`` objects, and see the
     320:ref:`generic views reference<ref-generic-views>` for the complete details).
     321
     322To pick a simple example, we might want to order a list of books by
     323publication date, with the most recent first:
     324
     325.. parsed-literal::
     326
     327    book_info = {
     328        "queryset" : Book.objects.all().order_by("-publication_date"),
     329    }
     330
     331    urlpatterns = patterns('',
     332        (r'^publishers/$', list_detail.object_list, publisher_info),
     333        **(r'^books/$', list_detail.object_list, book_info),**
     334    )
     335
     336
     337That's a pretty simple example, but it illustrates the idea nicely. Of course,
     338you'll usually want to do more than just reorder objects. If you want to
     339present a list of books by a particular publisher, you can use the same
     340technique:
     341
     342.. parsed-literal::
     343
     344    **acme_books = {**
     345        **"queryset": Book.objects.filter(publisher__name="Acme Publishing"),**
     346        **"template_name" : "books/acme_list.html"**
     347    **}**
     348
     349    urlpatterns = patterns('',
     350        (r'^publishers/$', list_detail.object_list, publisher_info),
     351        **(r'^books/acme/$', list_detail.object_list, acme_books),**
     352    )
     353
     354Notice that along with a filtered ``queryset``, we're also using a custom
     355template name. If we didn't, the generic view would use the same template as the
     356"vanilla" object list, which might not be what we want.
     357
     358Also notice that this isn't a very elegant way of doing publisher-specific
     359books. If we want to add another publisher page, we'd need another handful of
     360lines in the URLconf, and more than a few publishers would get unreasonable.
     361We'll deal with this problem in the next section.
     362
     363.. note::
     364
     365    If you get a 404 when requesting ``/books/acme/``, check to ensure you
     366    actually have a Publisher with the name 'ACME Publishing'.  Generic
     367    views have an ``allow_empty`` parameter for this case.  See the
     368    :ref:`generic views reference<ref-generic-views>` for more details.
     369
     370Complex filtering with wrapper functions
     371----------------------------------------
     372
     373Another common need is to filter down the objects given in a list page by some
     374key in the URL. Earlier we hard-coded the publisher's name in the URLconf, but
     375what if we wanted to write a view that displayed all the books by some arbitrary
     376publisher? We can "wrap" the ``object_list`` generic view to avoid writing a lot
     377of code by hand. As usual, we'll start by writing a URLconf:
     378
     379.. parsed-literal::
     380
     381    from mysite.books.views import books_by_publisher
     382
     383    urlpatterns = patterns('',
     384        (r'^publishers/$', list_detail.object_list, publisher_info),
     385        **(r'^books/(\w+)/$', books_by_publisher),**
     386    )
     387
     388Next, we'll write the ``books_by_publisher`` view itself::
     389
     390    from django.http import Http404
     391    from django.views.generic import list_detail
     392    from mysite.books.models import Book, Publisher
     393
     394    def books_by_publisher(request, name):
     395
     396        # Look up the publisher (and raise a 404 if it can't be found).
     397        try:
     398            publisher = Publisher.objects.get(name__iexact=name)
     399        except Publisher.DoesNotExist:
     400            raise Http404
     401
     402        # Use the object_list view for the heavy lifting.
     403        return list_detail.object_list(
     404            request,
     405            queryset = Book.objects.filter(publisher=publisher),
     406            template_name = "books/books_by_publisher.html",
     407            template_object_name = "books",
     408            extra_context = {"publisher" : publisher}
     409        )
     410
     411This works because there's really nothing special about generic views -- they're
     412just Python functions. Like any view function, generic views expect a certain
     413set of arguments and return ``HttpResponse`` objects. Thus, it's incredibly easy
     414to wrap a small function around a generic view that does additional work before
     415(or after; see the next section) handing things off to the generic view.
     416
     417.. note::
     418
     419    Notice that in the preceding example we passed the current publisher being
     420    displayed in the ``extra_context``. This is usually a good idea in wrappers
     421    of this nature; it lets the template know which "parent" object is currently
     422    being browsed.
     423
     424Performing extra work
     425---------------------
     426
     427The last common pattern we'll look at involves doing some extra work before
     428or after calling the generic view.
     429
     430Imagine we had a ``last_accessed`` field on our ``Author`` object that we were
     431using to keep track of the last time anybody looked at that author::
     432
     433    # models.py
     434
     435    class Author(models.Model):
     436        salutation = models.CharField(max_length=10)
     437        first_name = models.CharField(max_length=30)
     438        last_name = models.CharField(max_length=40)
     439        email = models.EmailField()
     440        headshot = models.ImageField(upload_to='/tmp')
     441        last_accessed = models.DateTimeField()
     442
     443The generic ``object_detail`` view, of course, wouldn't know anything about this
     444field, but once again we could easily write a custom view to keep that field
     445updated.
     446
     447First, we'd need to add an author detail bit in the URLconf to point to a
     448custom view:
     449
     450.. parsed-literal::
     451
     452    from mysite.books.views import author_detail
     453
     454    urlpatterns = patterns('',
     455        #...
     456        **(r'^authors/(?P<author_id>\d+)/$', author_detail),**
     457    )
     458
     459Then we'd write our wrapper function::
     460
     461    import datetime
     462    from mysite.books.models import Author
     463    from django.views.generic import list_detail
     464    from django.shortcuts import get_object_or_404
     465
     466    def author_detail(request, author_id):
     467        # Look up the Author (and raise a 404 if she's not found)
     468        author = get_object_or_404(Author, pk=author_id)
     469
     470        # Record the last accessed date
     471        author.last_accessed = datetime.datetime.now()
     472        author.save()
     473
     474        # Show the detail page
     475        return list_detail.object_detail(
     476            request,
     477            queryset = Author.objects.all(),
     478            object_id = author_id,
     479        )
     480
     481.. note::
     482
     483    This code won't actually work unless you create a
     484    ``books/author_detail.html`` template.
     485
     486We can use a similar idiom to alter the response returned by the generic view.
     487If we wanted to provide a downloadable plain-text version of the list of
     488authors, we could use a view like this::
     489
     490    def author_list_plaintext(request):
     491        response = list_detail.object_list(
     492            request,
     493            queryset = Author.objects.all(),
     494            mimetype = "text/plain",
     495            template_name = "books/author_list.txt"
     496        )
     497        response["Content-Disposition"] = "attachment; filename=authors.txt"
     498        return response
     499
     500This works because the generic views return simple ``HttpResponse`` objects
     501that can be treated like dictionaries to set HTTP headers. This
     502``Content-Disposition`` business, by the way, instructs the browser to
     503download and save the page instead of displaying it in the browser.
  • docs/topics/index.txt

    diff --git a/docs/topics/index.txt b/docs/topics/index.txt
    a b  
    1414   forms/index
    1515   forms/modelforms
    1616   templates
     17   generic-views
    1718   files
    1819   testing
    1920   auth
Back to Top