Ticket #3168: object_list_accept_list_v3.diff
File object_list_accept_list_v3.diff, 4.1 KB (added by , 18 years ago) |
---|
-
django/views/generic/list_detail.py
1 1 from django.template import loader, RequestContext 2 2 from django.http import Http404, HttpResponse 3 from django.db.models.query import QuerySet 3 4 from django.core.xheaders import populate_xheaders 4 5 from django.core.paginator import ObjectPaginator, InvalidPage 5 6 from django.core.exceptions import ObjectDoesNotExist 6 7 7 def object_list(request, queryset, paginate_by=None, page=None,8 allow_empty=False, template_name=None, template_loader=loader,8 def object_list(request, sequence=None, paginate_by=None, page=None, 9 allow_empty=False, queryset=None, template_name=None, template_loader=loader, 9 10 extra_context=None, context_processors=None, template_object_name='object', 10 11 mimetype=None): 11 12 """ 12 13 Generic list of objects. 13 14 14 Templates: ``<app_label>/<model_name>_list.html`` 15 Templates: ``<app_label>/<model_name>_list.html`` if queryset is a QuerySet 15 16 Context: 16 17 object_list 17 18 list of objects … … 40 41 the result number of the first object in the 41 42 object_list (1-indexed) 42 43 """ 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() 45 54 if paginate_by: 46 paginator = ObjectPaginator( queryset, paginate_by)55 paginator = ObjectPaginator(sequence, paginate_by) 47 56 if not page: 48 57 page = request.GET.get('page', 1) 49 58 try: … … 70 79 }, context_processors) 71 80 else: 72 81 c = RequestContext(request, { 73 '%s_list' % template_object_name: queryset,82 '%s_list' % template_object_name: sequence, 74 83 'is_paginated': False 75 84 }, context_processors) 76 if not allow_empty and len( queryset) == 0:85 if not allow_empty and len(sequence) == 0: 77 86 raise Http404 78 87 for key, value in extra_context.items(): 79 88 if callable(value): … … 81 90 else: 82 91 c[key] = value 83 92 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." 86 98 t = template_loader.get_template(template_name) 87 99 return HttpResponse(t.render(c), mimetype=mimetype) 88 100 -
docs/generic_views.txt
681 681 682 682 * ``queryset``: A ``QuerySet`` that represents the objects. 683 683 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 684 690 **Optional arguments:** 685 691 686 692 * ``paginate_by``: An integer specifying how many objects should be … … 721 727 722 728 If ``template_name`` isn't specified, this view will use the template 723 729 ``<app_label>/<model_name>_list.html`` by default. 730 **New in Django development version** This only applies if a ``QuerySet`` 731 is passed in. 724 732 725 733 **Template context:** 726 734