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

File generic-views-topics-docs.diff, 26.3 KB (added by Ramiro Morales, 10 years ago)
  • docs/index.txt

    diff --git a/docs/index.txt b/docs/index.txt
    a b  
    5757    * **The basics:** :ref:`URLconfs <topics-http-urls>` | :ref:`View functions <topics-http-views>` | :ref:`Shortcuts <topics-http-shortcuts>`
    5858    * **Reference:** :ref:`Request/response objects <ref-request-response>`
    5959    * **File uploads:** :ref:`Overview <topics-http-file-uploads>` | :ref:`File objects <ref-files-file>` | :ref:`Storage API <ref-files-storage>` | :ref:`Managing files <topics-files>` | :ref:`Custom storage <howto-custom-file-storage>`
    60     * **Advanced:** :ref:`Generic views <ref-generic-views>` | :ref:`Generating CSV <howto-outputting-csv>` | :ref:`Generating PDF <howto-outputting-pdf>`
     60    * **Generic views:** :ref:`Overview<topics-generic-views>` | :ref:`Included generic views<ref-generic-views>`
     61    * **Advanced:** :ref:`Generating CSV <howto-outputting-csv>` | :ref:`Generating PDF <howto-outputting-pdf>`
    6162    * **Middleware:** :ref:`Overview <topics-http-middleware>` | :ref:`Built-in middleware classes <ref-middleware>`
  • docs/internals/documentation.txt

    diff --git a/docs/internals/documentation.txt b/docs/internals/documentation.txt
    a b  
    131131The work is mostly done, but here's what's left, in rough order of priority.
    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.
    138133    * Change the "Added/changed in development version" callouts to proper
    139134      Sphinx ``.. versionadded::`` or ``.. versionchanged::`` directives.
  • docs/ref/generic-views.txt

    diff --git a/docs/ref/generic-views.txt b/docs/ref/generic-views.txt
    a b  
    44Generic views
    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.
    12 Django's generic views contain the following:
    14     * A set of views for doing list/detail interfaces.
    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).
    20     * A set of views for creating, editing, and deleting objects.
    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/
    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::
    33     from django.conf.urls.defaults import *
    34     from django_website.apps.blog.models import Entry
    36     info_dict = {
    37         'queryset': Entry.objects.all(),
    38         'date_field': 'pub_date',
    39     }
    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     )
    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.
    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
    6012Most generic views require the ``queryset`` key, which is a ``QuerySet``
    6113instance; see :ref:`topics-db-queries` for more information about ``QuerySet``
    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.
    7316"Simple" generic views
    802745        /objects/?page=3
    804     * To loop over all the available page numbers, use the ``page_range`` 
    805       variable. You can iterate over the list provided by ``page_range`` 
     747    * To loop over all the available page numbers, use the ``page_range``
     748      variable. You can iterate over the list provided by ``page_range``
    806749      to create a link to every page of results.
    808751These values and lists are 1-based, not 0-based, so the first page would be
    809 represented as page ``1``. 
     752represented as page ``1``.
    811754For more on pagination, read the :ref:`pagination documentation
    819762    /objects/?page=last
    821 This allows you to access the final page of results without first having to 
     764This allows you to access the final page of results without first having to
    822765determine how many pages there are.
    824767Note that ``page`` *must* be either a valid page number or the value ``last``;
    911854A page that displays a form for creating an object, redisplaying the form with
    912 validation errors (if there are any) and saving the object. 
     855validation errors (if there are any) and saving the object.
    914857**Required arguments:**
  • 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:
     4Generic views
     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
     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
     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.
     21Django ships with generic views to do the following:
     23    * Perform common "simple" tasks: redirect to a different page and
     24      render a given template.
     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.
     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.
     37    * Allow users to create, update, and delete objects -- with or
     38      without authorization.
     40Taken together, these views provide easy interfaces to perform the most common
     41tasks developers encounter.
     43Using generic views
     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.
     50For example, here's a simple URLconf you could use to present a static "about"
     53    from django.conf.urls.defaults import *
     54    from django.views.generic.simple import direct_to_template
     56    urlpatterns = patterns('',
     57        ('^about/$', direct_to_template, {
     58            'template': 'about.html'
     59        })
     60    )
     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
     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:
     73.. parsed-literal::
     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**
     79    urlpatterns = patterns('',
     80        ('^about/$', direct_to_template, {
     81            'template': 'about.html'
     82        }),
     83        **('^about/(\w+)/$', about_pages),**
     84    )
     86Next, we'll write the ``about_pages`` view::
     88    from django.http import Http404
     89    from django.template import TemplateDoesNotExist
     90    from django.views.generic.simple import direct_to_template
     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()
     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.
     104.. admonition:: Is there a security vulnerability here?
     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?
     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.
     119Generic views of objects
     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.
     127Let's take a look at one of these generic views: the "object list" view. We'll
     128be using these models::
     130    # models.py
     131    from django.db import models
     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()
     141        def __unicode__(self):
     142            return self.name
     144        class Meta:
     145            ordering = ["-name"]
     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()
     153To build a list page of all books, we'd use a URLconf along these lines::
     155    from django.conf.urls.defaults import *
     156    from django.views.generic import list_detail
     157    from mysite.books.models import Publisher
     159    publisher_info = {
     160        "queryset" : Publisher.objects.all(),
     161    }
     163    urlpatterns = patterns('',
     164        (r'^publishers/$', list_detail.object_list, publisher_info)
     165    )
     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.
     176.. highlightlang:: html+django
     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::
     182    {% extends "base.html" %}
     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 %}
     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.
     199Extending generic views
     202.. highlightlang:: python
     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
     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.
     214Making "friendly" template contexts
     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.
     223We can change the name of that variable easily with the ``template_object_name``
     226.. parsed-literal::
     228    publisher_info = {
     229        "queryset" : Publisher.objects.all(),
     230        **"template_object_name" : "publisher",**
     231    }
     233    urlpatterns = patterns('',
     234        (r'^publishers/$', list_detail.object_list, publisher_info)
     235    )
     237Providing a useful ``template_object_name`` is always a good idea. Your
     238coworkers who design templates will thank you.
     240Adding extra context
     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.
     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:
     254.. parsed-literal::
     256    from mysite.books.models import Publisher, **Book**
     258    publisher_info = {
     259        "queryset" : Publisher.objects.all(),
     260        "template_object_name" : "publisher",
     261        **"extra_context" : {"book_list" : Book.objects.all()}**
     262    }
     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.
     268However, there's actually a subtle bug here -- can you spot it?
     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).
     277.. note::
     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.
     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:
     288.. parsed-literal::
     290    def get_books():
     291        return Book.objects.all()
     293    publisher_info = {
     294        "queryset" : Publisher.objects.all(),
     295        "template_object_name" : "publisher",
     296        "extra_context" : **{"book_list" : get_books}**
     297    }
     299or you could use a less obvious but shorter version that relies on the fact that
     300``Publisher.objects.all`` is itself a callable:
     302.. parsed-literal::
     304    publisher_info = {
     305        "queryset" : Publisher.objects.all(),
     306        "template_object_name" : "publisher",
     307        "extra_context" : **{"book_list" : Book.objects.all}**
     308    }
     310Notice the lack of parentheses after ``Book.objects.all``; this references
     311the function without actually calling it (which the generic view will do later).
     313Viewing subsets of objects
     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).
     322To pick a simple example, we might want to order a list of books by
     323publication date, with the most recent first:
     325.. parsed-literal::
     327    book_info = {
     328        "queryset" : Book.objects.all().order_by("-publication_date"),
     329    }
     331    urlpatterns = patterns('',
     332        (r'^publishers/$', list_detail.object_list, publisher_info),
     333        **(r'^books/$', list_detail.object_list, book_info),**
     334    )
     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
     342.. parsed-literal::
     344    **acme_books = {**
     345        **"queryset": Book.objects.filter(publisher__name="Acme Publishing"),**
     346        **"template_name" : "books/acme_list.html"**
     347    **}**
     349    urlpatterns = patterns('',
     350        (r'^publishers/$', list_detail.object_list, publisher_info),
     351        **(r'^books/acme/$', list_detail.object_list, acme_books),**
     352    )
     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.
     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.
     363.. note::
     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.
     370Complex filtering with wrapper functions
     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:
     379.. parsed-literal::
     381    from mysite.books.views import books_by_publisher
     383    urlpatterns = patterns('',
     384        (r'^publishers/$', list_detail.object_list, publisher_info),
     385        **(r'^books/(\w+)/$', books_by_publisher),**
     386    )
     388Next, we'll write the ``books_by_publisher`` view itself::
     390    from django.http import Http404
     391    from django.views.generic import list_detail
     392    from mysite.books.models import Book, Publisher
     394    def books_by_publisher(request, name):
     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
     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        )
     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.
     417.. note::
     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.
     424Performing extra work
     427The last common pattern we'll look at involves doing some extra work before
     428or after calling the generic view.
     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::
     433    # models.py
     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()
     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
     447First, we'd need to add an author detail bit in the URLconf to point to a
     448custom view:
     450.. parsed-literal::
     452    from mysite.books.views import author_detail
     454    urlpatterns = patterns('',
     455        #...
     456        **(r'^authors/(?P<author_id>\d+)/$', author_detail),**
     457    )
     459Then we'd write our wrapper function::
     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
     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)
     470        # Record the last accessed date
     471        author.last_accessed = datetime.datetime.now()
     472        author.save()
     474        # Show the detail page
     475        return list_detail.object_detail(
     476            request,
     477            queryset = Author.objects.all(),
     478            object_id = author_id,
     479        )
     481.. note::
     483    This code won't actually work unless you create a
     484    ``books/author_detail.html`` template.
     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::
     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
     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