Ticket #3168: patch.diff

File patch.diff, 3.4 KB (added by Grimboy, 17 years ago)
  • django/views/generic/list_detail.py

     
    11from django.template import loader, RequestContext
    22from django.http import Http404, HttpResponse
     3from django.db.models.query import QuerySet
    34from django.core.xheaders import populate_xheaders
    45from django.core.paginator import ObjectPaginator, InvalidPage
    56from django.core.exceptions import ObjectDoesNotExist
     
    1112    """
    1213    Generic list of objects.
    1314
    14     Templates: ``<app_label>/<model_name>_list.html``
     15    Templates: ``<app_label>/<model_name>_list.html`` if queryset is a QuerySet
    1516    Context:
    1617        object_list
    1718            list of objects
     
    3435        hits
    3536            number of objects, total
    3637    """
    37     if extra_context is None: extra_context = {}
    38     queryset = queryset._clone()
     38    if extra_context is None:
     39        extra_context = {}
     40    if type(queryset) == QuerySet:
     41        queryset = queryset._clone()
    3942    if paginate_by:
    4043        paginator = ObjectPaginator(queryset, paginate_by)
    4144        if not page:
     
    7376        else:
    7477            c[key] = value
    7578    if not template_name:
    76         model = queryset.model
    77         template_name = "%s/%s_list.html" % (model._meta.app_label, model._meta.object_name.lower())
     79        if type(queryset) == QuerySet:
     80            model = queryset.model
     81            template_name = "%s/%s_list.html" % (model._meta.app_label, model._meta.object_name.lower())
     82        else:
     83            raise AttributeError, "Generic list view must be called with either a template_name or a queryset (rather than a list)."
    7884    t = template_loader.get_template(template_name)
    7985    return HttpResponse(t.render(c), mimetype=mimetype)
    8086
  • docs/generic_views.txt

     
    679679
    680680**Required arguments:**
    681681
    682     * ``queryset``: A ``QuerySet`` that represents the objects.
     682    * ``queryset``: A ``QuerySet`` or other sequence such as a list that
     683      represents the objects.
    683684
     685    * Either the argument ``queryset`` should be a ``QuerySet`` or the
     686      argument ``template_name`` should be supplied. ``template_name`` is the
     687      full name of a template to use in rendering the page. This also lets you
     688      override the default template name if you are using a ``QuerySet`` for
     689      ``queryset`` (see below).
     690
    684691**Optional arguments:**
    685692
    686693    * ``paginate_by``: An integer specifying how many objects should be
     
    690697      number, or a ``page`` variable specified in the URLconf. See
    691698      "Notes on pagination" below.
    692699
    693     * ``template_name``: The full name of a template to use in rendering the
    694       page. This lets you override the default template name (see below).
    695 
    696700    * ``template_loader``: The template loader to use when loading the
    697701      template. By default, it's ``django.template.loader``.
    698702
     
    719723
    720724**Template name:**
    721725
    722 If ``template_name`` isn't specified, this view will use the template
    723 ``<app_label>/<model_name>_list.html`` by default.
     726If ``template_name`` isn't specified and ``queryset`` is a ``QuerySet``, this
     727view will use the template ``<app_label>/<model_name>_list.html`` by default.
    724728
    725729**Template context:**
    726730
Back to Top