Ticket #3168: object_list_accept_list_v3.diff

File object_list_accept_list_v3.diff, 4.1 KB (added by frankie@…, 18 years ago)

Fixed silly bug from using if foo rather than if foo is not None. Against [4750].

  • 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
    67
    7 def object_list(request, queryset, paginate_by=None, page=None,
    8         allow_empty=False, template_name=None, template_loader=loader,
     8def object_list(request, sequence=None, paginate_by=None, page=None,
     9        allow_empty=False, queryset=None, template_name=None, template_loader=loader,
    910        extra_context=None, context_processors=None, template_object_name='object',
    1011        mimetype=None):
    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
     
    4041            the result number of the first object in the
    4142            object_list (1-indexed)
    4243    """
    43     if extra_context is None: extra_context = {}
    44     queryset = queryset._clone()
     44    if extra_context is None:
     45        extra_context = {}
     46    if (queryset is not None and sequence is not None) or \
     47        not (queryset is not None or sequence is not None):
     48        raise (AttributeError,
     49        ), "One and only one of queryset and sequence needto be specified."
     50    if queryset:
     51        sequence = queryset
     52    if type(sequence) == QuerySet:
     53        sequence = sequence._clone()
    4554    if paginate_by:
    46         paginator = ObjectPaginator(queryset, paginate_by)
     55        paginator = ObjectPaginator(sequence, paginate_by)
    4756        if not page:
    4857            page = request.GET.get('page', 1)
    4958        try:
     
    7079        }, context_processors)
    7180    else:
    7281        c = RequestContext(request, {
    73             '%s_list' % template_object_name: queryset,
     82            '%s_list' % template_object_name: sequence,
    7483            'is_paginated': False
    7584        }, context_processors)
    76         if not allow_empty and len(queryset) == 0:
     85        if not allow_empty and len(sequence) == 0:
    7786            raise Http404
    7887    for key, value in extra_context.items():
    7988        if callable(value):
     
    8190        else:
    8291            c[key] = value
    8392    if not template_name:
    84         model = queryset.model
    85         template_name = "%s/%s_list.html" % (model._meta.app_label, model._meta.object_name.lower())
     93        if type(sequence) == QuerySet:
     94            model = sequence.model
     95            template_name = "%s/%s_list.html" % (model._meta.app_label, model._meta.object_name.lower())
     96        else:
     97            raise AttributeError, "Generic list view must be called with either a template_name or a QuerySet or both."
    8698    t = template_loader.get_template(template_name)
    8799    return HttpResponse(t.render(c), mimetype=mimetype)
    88100
  • docs/generic_views.txt

     
    681681
    682682    * ``queryset``: A ``QuerySet`` that represents the objects.
    683683
     684    * ``sequence``: **New in Django development version**
     685      A sequence, such as a list or tuple, that represents the objects. If
     686      ``queryset`` is supplied then this argument should not be and if this
     687      argument is supplied then ``queryset`` should not be.. If a ``QuerySet``
     688      isn't supplied then then the ``template_name`` argument is required.
     689
    684690**Optional arguments:**
    685691
    686692    * ``paginate_by``: An integer specifying how many objects should be
     
    721727
    722728If ``template_name`` isn't specified, this view will use the template
    723729``<app_label>/<model_name>_list.html`` by default.
     730**New in Django development version** This only applies if a ``QuerySet``
     731is passed in.
    724732
    725733**Template context:**
    726734
Back to Top