Ticket #2367: generic_views_pagination.diff
File generic_views_pagination.diff, 15.2 KB (added by , 18 years ago) |
---|
-
django/core/paginator.py
1 from django.http import Http404 2 1 3 class InvalidPage(Exception): 2 4 pass 3 5 … … 86 88 87 89 hits = property(_get_hits) 88 90 pages = property(_get_pages) 91 92 def compute_pagination(paginator, page, paginate_by): 93 """ 94 Compute queryset considering pagination, and update the context. 95 """ 96 try: 97 page = int(page) 98 queryset = paginator.get_page(page - 1) 99 except (InvalidPage, ValueError): 100 if page == 1 and allow_empty: 101 queryset = [] 102 else: 103 raise Http404 104 105 c = {} 106 c['is_paginated'] = paginator.pages > 1 107 c['results_per_page'] = paginate_by 108 c['has_next'] = paginator.has_next_page(page - 1) 109 c['has_previous'] = paginator.has_previous_page(page - 1) 110 c['page'] = page 111 c['next'] = page + 1 112 c['previous'] = page - 1 113 c['last_on_page'] = paginator.last_on_page(page - 1), 114 c['first_on_page'] = paginator.first_on_page(page - 1), 115 c['pages'] = paginator.pages 116 c['hits'] = paginator.hits 117 118 return (queryset, c) -
django/views/generic/date_based.py
1 1 from django.template import loader, RequestContext 2 2 from django.core.exceptions import ObjectDoesNotExist 3 3 from django.core.xheaders import populate_xheaders 4 from django.core.paginator import compute_pagination, ObjectPaginator 4 5 from django.db.models.fields import DateTimeField 5 6 from django.http import Http404, HttpResponse 6 7 import datetime, time … … 8 9 def archive_index(request, queryset, date_field, num_latest=15, 9 10 template_name=None, template_loader=loader, 10 11 extra_context=None, allow_empty=False, context_processors=None, 11 mimetype=None, allow_future=False ):12 mimetype=None, allow_future=False, paginate_by=None, page=None): 12 13 """ 13 14 Generic top-level archive of date-based objects. 14 15 … … 18 19 List of years 19 20 latest 20 21 Latest N (defaults to 15) objects by date 22 is_paginated 23 are the results paginated? 24 results_per_page 25 number of objects per page (if paginated) 26 has_next 27 is there a next page? 28 has_previous 29 is there a prev page? 30 page 31 the current page 32 next 33 the next page 34 previous 35 the previous page 36 pages 37 number of pages, total 38 hits 39 number of objects, total 40 last_on_page 41 the result number of the last of object in the 42 object_list (1-indexed) 43 first_on_page 44 the result number of the first object in the 45 object_list (1-indexed) 21 46 """ 22 47 if extra_context is None: extra_context = {} 23 48 model = queryset.model … … 30 55 if date_list and num_latest: 31 56 latest = queryset.order_by('-'+date_field)[:num_latest] 32 57 else: 33 latest = None58 latest = [] 34 59 60 # Pagination. 61 pagination_context = {'is_paginated': False} 62 if paginate_by: 63 paginator = ObjectPaginator(latest, paginate_by) 64 page = page or request.GET.get('page', 1) 65 latest, pagination_context = compute_pagination(paginator, page, paginate_by) 66 35 67 if not template_name: 36 68 template_name = "%s/%s_archive.html" % (model._meta.app_label, model._meta.object_name.lower()) 37 69 t = template_loader.get_template(template_name) … … 44 76 c[key] = value() 45 77 else: 46 78 c[key] = value 79 80 c.update(pagination_context) 81 47 82 return HttpResponse(t.render(c), mimetype=mimetype) 48 83 49 84 def archive_year(request, year, queryset, date_field, template_name=None, 50 85 template_loader=loader, extra_context=None, allow_empty=False, 51 86 context_processors=None, template_object_name='object', mimetype=None, 52 make_object_list=False, allow_future=False ):87 make_object_list=False, allow_future=False, paginate_by=None, page=None): 53 88 """ 54 89 Generic yearly archive view. 55 90 … … 61 96 This year 62 97 object_list 63 98 List of objects published in the given month 99 64 100 (Only available if make_object_list argument is True) 101 102 is_paginated 103 are the results paginated? 104 results_per_page 105 number of objects per page (if paginated) 106 has_next 107 is there a next page? 108 has_previous 109 is there a prev page? 110 page 111 the current page 112 next 113 the next page 114 previous 115 the previous page 116 pages 117 number of pages, total 118 hits 119 number of objects, total 120 last_on_page 121 the result number of the last of object in the 122 object_list (1-indexed) 123 first_on_page 124 the result number of the first object in the 125 object_list (1-indexed) 65 126 """ 66 127 if extra_context is None: extra_context = {} 67 128 model = queryset.model … … 79 140 object_list = queryset.filter(**lookup_kwargs).order_by(date_field) 80 141 else: 81 142 object_list = [] 143 144 # Pagination. 145 pagination_context = {'is_paginated': False} 146 if paginate_by: 147 paginator = ObjectPaginator(object_list, paginate_by) 148 page = page or request.GET.get('page', 1) 149 object_list, pagination_context = compute_pagination(paginator, page, paginate_by) 150 82 151 if not template_name: 83 152 template_name = "%s/%s_archive_year.html" % (model._meta.app_label, model._meta.object_name.lower()) 84 153 t = template_loader.get_template(template_name) … … 92 161 c[key] = value() 93 162 else: 94 163 c[key] = value 164 165 c.update(pagination_context) 166 95 167 return HttpResponse(t.render(c), mimetype=mimetype) 96 168 97 169 def archive_month(request, year, month, queryset, date_field, 98 170 month_format='%b', template_name=None, template_loader=loader, 99 171 extra_context=None, allow_empty=False, context_processors=None, 100 template_object_name='object', mimetype=None, allow_future=False): 172 template_object_name='object', mimetype=None, allow_future=False, 173 paginate_by=None, page=None): 101 174 """ 102 175 Generic monthly archive view. 103 176 … … 111 184 (date) the first day of the previous month 112 185 object_list: 113 186 list of objects published in the given month 187 is_paginated 188 are the results paginated? 189 results_per_page 190 number of objects per page (if paginated) 191 has_next 192 is there a next page? 193 has_previous 194 is there a prev page? 195 page 196 the current page 197 next 198 the next page 199 previous 200 the previous page 201 pages 202 number of pages, total 203 hits 204 number of objects, total 205 last_on_page 206 the result number of the last of object in the 207 object_list (1-indexed) 208 first_on_page 209 the result number of the first object in the 210 object_list (1-indexed) 114 211 """ 115 212 if extra_context is None: extra_context = {} 116 213 try: … … 144 241 else: 145 242 next_month = None 146 243 244 # Pagination. 245 pagination_context = {'is_paginated': False} 246 if paginate_by: 247 paginator = ObjectPaginator(object_list, paginate_by) 248 page = page or request.GET.get('page', 1) 249 object_list, pagination_context = compute_pagination(paginator, page, paginate_by) 250 147 251 if not template_name: 148 252 template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower()) 149 253 t = template_loader.get_template(template_name) … … 158 262 c[key] = value() 159 263 else: 160 264 c[key] = value 265 266 c.update(pagination_context) 267 161 268 return HttpResponse(t.render(c), mimetype=mimetype) 162 269 163 270 def archive_week(request, year, week, queryset, date_field, 164 271 template_name=None, template_loader=loader, 165 272 extra_context=None, allow_empty=True, context_processors=None, 166 template_object_name='object', mimetype=None, allow_future=False): 273 template_object_name='object', mimetype=None, allow_future=False, 274 paginate_by=None, page=None): 167 275 """ 168 276 Generic weekly archive view. 169 277 … … 173 281 (date) this week 174 282 object_list: 175 283 list of objects published in the given week 284 is_paginated 285 are the results paginated? 286 results_per_page 287 number of objects per page (if paginated) 288 has_next 289 is there a next page? 290 has_previous 291 is there a prev page? 292 page 293 the current page 294 next 295 the next page 296 previous 297 the previous page 298 pages 299 number of pages, total 300 hits 301 number of objects, total 302 last_on_page 303 the result number of the last of object in the 304 object_list (1-indexed) 305 first_on_page 306 the result number of the first object in the 307 object_list (1-indexed) 176 308 """ 177 309 if extra_context is None: extra_context = {} 178 310 try: … … 194 326 object_list = queryset.filter(**lookup_kwargs) 195 327 if not object_list and not allow_empty: 196 328 raise Http404 329 330 # Pagination. 331 pagination_context = {'is_paginated': False} 332 if paginate_by: 333 paginator = ObjectPaginator(object_list, paginate_by) 334 page = page or request.GET.get('page', 1) 335 object_list, pagination_context = compute_pagination(paginator, page, paginate_by) 336 197 337 if not template_name: 198 338 template_name = "%s/%s_archive_week.html" % (model._meta.app_label, model._meta.object_name.lower()) 199 339 t = template_loader.get_template(template_name) … … 206 346 c[key] = value() 207 347 else: 208 348 c[key] = value 349 350 c.update(pagination_context) 351 209 352 return HttpResponse(t.render(c), mimetype=mimetype) 210 353 211 354 def archive_day(request, year, month, day, queryset, date_field, 212 355 month_format='%b', day_format='%d', template_name=None, 213 356 template_loader=loader, extra_context=None, allow_empty=False, 214 357 context_processors=None, template_object_name='object', 215 mimetype=None, allow_future=False ):358 mimetype=None, allow_future=False, paginate_by=None, page=None): 216 359 """ 217 360 Generic daily archive view. 218 361 … … 226 369 (datetime) the previous day 227 370 next_day 228 371 (datetime) the next day, or None if the current day is today 372 is_paginated 373 are the results paginated? 374 results_per_page 375 number of objects per page (if paginated) 376 has_next 377 is there a next page? 378 has_previous 379 is there a prev page? 380 page 381 the current page 382 next 383 the next page 384 previous 385 the previous page 386 pages 387 number of pages, total 388 hits 389 number of objects, total 390 last_on_page 391 the result number of the last of object in the 392 object_list (1-indexed) 393 first_on_page 394 the result number of the first object in the 395 object_list (1-indexed) 229 396 """ 230 397 if extra_context is None: extra_context = {} 231 398 try: … … 256 423 else: 257 424 next_day = None 258 425 426 # Pagination. 427 pagination_context = {'is_paginated': False} 428 if paginate_by: 429 paginator = ObjectPaginator(object_list, paginate_by) 430 page = page or request.GET.get('page', 1) 431 object_list, pagination_context = compute_pagination(paginator, page, paginate_by) 432 259 433 if not template_name: 260 434 template_name = "%s/%s_archive_day.html" % (model._meta.app_label, model._meta.object_name.lower()) 261 435 t = template_loader.get_template(template_name) … … 270 444 c[key] = value() 271 445 else: 272 446 c[key] = value 447 448 c.update(pagination_context) 449 273 450 return HttpResponse(t.render(c), mimetype=mimetype) 274 451 275 452 def archive_today(request, **kwargs): -
django/views/generic/list_detail.py
1 1 from django.template import loader, RequestContext 2 2 from django.http import Http404, HttpResponse 3 3 from django.core.xheaders import populate_xheaders 4 from django.core.paginator import ObjectPaginator, InvalidPage4 from django.core.paginator import compute_pagination, ObjectPaginator 5 5 from django.core.exceptions import ObjectDoesNotExist 6 6 7 7 def object_list(request, queryset, paginate_by=None, page=None, … … 42 42 """ 43 43 if extra_context is None: extra_context = {} 44 44 queryset = queryset._clone() 45 46 if not allow_empty and len(queryset) == 0: 47 raise Http404 48 49 # Pagination. 50 pagination_context = {'is_paginated': False} 45 51 if paginate_by: 46 52 paginator = ObjectPaginator(queryset, paginate_by) 47 if not page: 48 page = request.GET.get('page', 1) 49 try: 50 page = int(page) 51 object_list = paginator.get_page(page - 1) 52 except (InvalidPage, ValueError): 53 if page == 1 and allow_empty: 54 object_list = [] 55 else: 56 raise Http404 57 c = RequestContext(request, { 58 '%s_list' % template_object_name: object_list, 59 'is_paginated': paginator.pages > 1, 60 'results_per_page': paginate_by, 61 'has_next': paginator.has_next_page(page - 1), 62 'has_previous': paginator.has_previous_page(page - 1), 63 'page': page, 64 'next': page + 1, 65 'previous': page - 1, 66 'last_on_page': paginator.last_on_page(page - 1), 67 'first_on_page': paginator.first_on_page(page - 1), 68 'pages': paginator.pages, 69 'hits' : paginator.hits, 70 }, context_processors) 71 else: 72 c = RequestContext(request, { 73 '%s_list' % template_object_name: queryset, 74 'is_paginated': False 75 }, context_processors) 76 if not allow_empty and len(queryset) == 0: 77 raise Http404 53 page = page or request.GET.get('page', 1) 54 queryset, pagination_context = compute_pagination(paginator, page, paginate_by) 55 56 c = RequestContext(request, { 57 '%s_list' % template_object_name: queryset, 58 }, context_processors) 78 59 for key, value in extra_context.items(): 79 60 if callable(value): 80 61 c[key] = value() 81 62 else: 82 63 c[key] = value 64 65 c.update(pagination_context) 66 83 67 if not template_name: 84 68 model = queryset.model 85 69 template_name = "%s/%s_list.html" % (model._meta.app_label, model._meta.object_name.lower())