Ticket #11554: 11554.diff

File 11554.diff, 3.1 KB (added by ramiro, 6 years ago)

Patch for this ticket, applies cleanly to thr 1.0.x branch too.

  • docs/topics/generic-views.txt

    diff -r 636f020aaf8e docs/topics/generic-views.txt
    a b  
    150150        publisher = models.ForeignKey(Publisher)
    151151        publication_date = models.DateField()
    152152
    153 To build a list page of all books, we'd use a URLconf along these lines::
     153To build a list page of all publishers, we'd use a URLconf along these lines::
    154154
    155155    from django.conf.urls.defaults import *
    156156    from django.views.generic import list_detail
     
    176176.. highlightlang:: html+django
    177177
    178178This template will be rendered against a context containing a variable called
    179 ``object_list`` that contains all the book objects. A very simple template
     179``object_list`` that contains all the publisher objects. A very simple template
    180180might look like the following::
    181181
    182182    {% extends "base.html" %}
     
    217217You might have noticed that our sample publisher list template stores all the
    218218books in a variable named ``object_list``. While this works just fine, it isn't
    219219all that "friendly" to template authors: they have to "just know" that they're
    220 dealing with books here. A better name for that variable would be
     220dealing with publishers here. A better name for that variable would be
    221221``publisher_list``; that variable's content is pretty obvious.
    222222
    223223We can change the name of that variable easily with the ``template_object_name``
     
    241241--------------------
    242242
    243243Often you simply need to present some extra information beyond that provided by
    244 the generic view. For example, think of showing a list of all the other
    245 publishers on each publisher detail page. The ``object_detail`` generic view
    246 provides the publisher to the context, but it seems there's no way to get a list
    247 of *all* publishers in that template.
     244the generic view. For example, think of showing a list of all the books on each
     245publisher detail page. The ``object_detail`` generic view provides the
     246publisher to the context, but it seems there's no way to get additional
     247information in that template.
    248248
    249249But there is: all generic views take an extra optional parameter,
    250250``extra_context``. This is a dictionary of extra objects that will be added to
    251 the template's context. So, to provide the list of all publishers on the detail
     251the template's context. So, to provide the list of all books on the detail
    252252detail view, we'd use an info dict like this:
    253253
    254254.. parsed-literal::
     
    268268However, there's actually a subtle bug here -- can you spot it?
    269269
    270270The problem has to do with when the queries in ``extra_context`` are evaluated.
    271 Because this example puts ``Publisher.objects.all()`` in the URLconf, it will
     271Because this example puts ``Book.objects.all()`` in the URLconf, it will
    272272be evaluated only once (when the URLconf is first loaded). Once you add or
    273 remove publishers, you'll notice that the generic view doesn't reflect those
     273remove books, you'll notice that the generic view doesn't reflect those
    274274changes until you reload the Web server (see :ref:`caching-and-querysets`
    275275for more information about when QuerySets are cached and evaluated).
    276276
Back to Top