Ticket #3168: object_list_accept_list_v2.diff
File object_list_accept_list_v2.diff, 4.0 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 … … 34 35 hits 35 36 number of objects, total 36 37 """ 37 if extra_context is None: extra_context = {} 38 queryset = queryset._clone() 38 if extra_context is None: 39 extra_context = {} 40 if (queryset and sequence) or not (queryset or sequence): 41 raise AttributeError, "One and only one of queryset and sequence need to be specified." 42 if queryset: 43 sequence = queryset 44 if type(sequence) == QuerySet: 45 sequence = sequence._clone() 39 46 if paginate_by: 40 paginator = ObjectPaginator( queryset, paginate_by)47 paginator = ObjectPaginator(sequence, paginate_by) 41 48 if not page: 42 49 page = request.GET.get('page', 1) 43 50 try: … … 62 69 }, context_processors) 63 70 else: 64 71 c = RequestContext(request, { 65 '%s_list' % template_object_name: queryset,72 '%s_list' % template_object_name: sequence, 66 73 'is_paginated': False 67 74 }, context_processors) 68 if not allow_empty and len( queryset) == 0:75 if not allow_empty and len(sequence) == 0: 69 76 raise Http404 70 77 for key, value in extra_context.items(): 71 78 if callable(value): … … 73 80 else: 74 81 c[key] = value 75 82 if not template_name: 76 model = queryset.model 77 template_name = "%s/%s_list.html" % (model._meta.app_label, model._meta.object_name.lower()) 83 if type(sequence) == QuerySet: 84 model = sequence.model 85 template_name = "%s/%s_list.html" % (model._meta.app_label, model._meta.object_name.lower()) 86 else: 87 raise AttributeError, "Generic list view must be called with either a template_name or a QuerySet or both." 78 88 t = template_loader.get_template(template_name) 79 89 return HttpResponse(t.render(c), mimetype=mimetype) 80 90 -
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