Skip to content

Commit

Permalink
Added allow_empty hook to archive_index date-based generic view.
Browse files Browse the repository at this point in the history
git-svn-id: http://code.djangoproject.com/svn/django/trunk@1510 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
adrianholovaty committed Nov 30, 2005
1 parent 0c5cf18 commit bf0f6ec
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
10 changes: 5 additions & 5 deletions django/views/generic/date_based.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


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.
Expand All @@ -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,),
Expand Down Expand Up @@ -140,7 +140,7 @@ def archive_month(request, year, month, app_label, module_name, date_field,


def archive_day(request, year, month, day, app_label, module_name, date_field, def archive_day(request, year, month, day, app_label, module_name, date_field,
month_format='%b', day_format='%d', template_name=None, month_format='%b', day_format='%d', template_name=None,
template_loader=template_loader, extra_lookup_kwargs={}, template_loader=template_loader, extra_lookup_kwargs={},
extra_context={}, allow_empty=False): extra_context={}, allow_empty=False):
""" """
Generic daily archive view. Generic daily archive view.
Expand Down Expand Up @@ -204,7 +204,7 @@ def archive_today(request, **kwargs):
def object_detail(request, year, month, day, app_label, module_name, date_field, def object_detail(request, year, month, day, app_label, module_name, date_field,
month_format='%b', day_format='%d', object_id=None, slug=None, month_format='%b', day_format='%d', object_id=None, slug=None,
slug_field=None, template_name=None, template_name_field=None, slug_field=None, template_name=None, template_name_field=None,
template_loader=template_loader, extra_lookup_kwargs={}, template_loader=template_loader, extra_lookup_kwargs={},
extra_context={}): extra_context={}):
""" """
Generic detail view from year/month/day/slug or year/month/day/id structure. Generic detail view from year/month/day/slug or year/month/day/id structure.
Expand Down
18 changes: 15 additions & 3 deletions docs/generic_views.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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.


Expand Down

0 comments on commit bf0f6ec

Please sign in to comment.