Added allow_empty hook to archive_index date-based generic view.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1510 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Adrian Holovaty 2005-11-30 22:03:50 +00:00
parent 0c5cf18e1d
commit bf0f6ec3b4
2 changed files with 20 additions and 8 deletions

View File

@ -8,7 +8,7 @@ import datetime, time
def archive_index(request, app_label, module_name, date_field, num_latest=15, def archive_index(request, app_label, module_name, date_field, num_latest=15,
template_name=None, template_loader=template_loader, template_name=None, template_loader=template_loader,
extra_lookup_kwargs={}, extra_context={}): extra_lookup_kwargs={}, extra_context={}, allow_empty=False):
""" """
Generic top-level archive of date-based objects. Generic top-level archive of date-based objects.
@ -23,10 +23,10 @@ def archive_index(request, app_label, module_name, date_field, num_latest=15,
lookup_kwargs = {'%s__lte' % date_field: datetime.datetime.now()} lookup_kwargs = {'%s__lte' % date_field: datetime.datetime.now()}
lookup_kwargs.update(extra_lookup_kwargs) lookup_kwargs.update(extra_lookup_kwargs)
date_list = getattr(mod, "get_%s_list" % date_field)('year', **lookup_kwargs)[::-1] date_list = getattr(mod, "get_%s_list" % date_field)('year', **lookup_kwargs)[::-1]
if not date_list: if not date_list and not allow_empty:
raise Http404("No %s.%s available" % (app_label, module_name)) raise Http404("No %s.%s available" % (app_label, module_name))
if num_latest: if date_list and num_latest:
lookup_kwargs.update({ lookup_kwargs.update({
'limit': num_latest, 'limit': num_latest,
'order_by': ('-' + date_field,), 'order_by': ('-' + date_field,),

View File

@ -135,9 +135,21 @@ arguments:
The date-based generic functions are: The date-based generic functions are:
``archive_index`` ``archive_index``
A top-level index page showing the "latest" objects. Has an optional A top-level index page showing the "latest" objects.
argument, ``num_latest``, which is the number of items to display on the
page (defaults to 15). Takes the following optional arguments:
======================= =================================================
Argument Description
======================= =================================================
``num_latest`` The number of items to display on the page.
Defaults to 15.
``allow_empty`` **New in Django development version.**
If ``False`` and there are no objects to display,
the view will raise a 404 instead of displaying
an empty index page. ``False`` is default.
======================= =================================================
Uses the template ``app_label/module_name_archive`` by default. Uses the template ``app_label/module_name_archive`` by default.