Skip to content

Commit bf0f6ec

Browse files
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
1 parent 0c5cf18 commit bf0f6ec

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

django/views/generic/date_based.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
def archive_index(request, app_label, module_name, date_field, num_latest=15,
1010
template_name=None, template_loader=template_loader,
11-
extra_lookup_kwargs={}, extra_context={}):
11+
extra_lookup_kwargs={}, extra_context={}, allow_empty=False):
1212
"""
1313
Generic top-level archive of date-based objects.
1414
@@ -23,10 +23,10 @@ def archive_index(request, app_label, module_name, date_field, num_latest=15,
2323
lookup_kwargs = {'%s__lte' % date_field: datetime.datetime.now()}
2424
lookup_kwargs.update(extra_lookup_kwargs)
2525
date_list = getattr(mod, "get_%s_list" % date_field)('year', **lookup_kwargs)[::-1]
26-
if not date_list:
26+
if not date_list and not allow_empty:
2727
raise Http404("No %s.%s available" % (app_label, module_name))
2828

29-
if num_latest:
29+
if date_list and num_latest:
3030
lookup_kwargs.update({
3131
'limit': num_latest,
3232
'order_by': ('-' + date_field,),
@@ -140,7 +140,7 @@ def archive_month(request, year, month, app_label, module_name, date_field,
140140

141141
def archive_day(request, year, month, day, app_label, module_name, date_field,
142142
month_format='%b', day_format='%d', template_name=None,
143-
template_loader=template_loader, extra_lookup_kwargs={},
143+
template_loader=template_loader, extra_lookup_kwargs={},
144144
extra_context={}, allow_empty=False):
145145
"""
146146
Generic daily archive view.
@@ -204,7 +204,7 @@ def archive_today(request, **kwargs):
204204
def object_detail(request, year, month, day, app_label, module_name, date_field,
205205
month_format='%b', day_format='%d', object_id=None, slug=None,
206206
slug_field=None, template_name=None, template_name_field=None,
207-
template_loader=template_loader, extra_lookup_kwargs={},
207+
template_loader=template_loader, extra_lookup_kwargs={},
208208
extra_context={}):
209209
"""
210210
Generic detail view from year/month/day/slug or year/month/day/id structure.

docs/generic_views.txt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,21 @@ arguments:
135135
The date-based generic functions are:
136136

137137
``archive_index``
138-
A top-level index page showing the "latest" objects. Has an optional
139-
argument, ``num_latest``, which is the number of items to display on the
140-
page (defaults to 15).
138+
A top-level index page showing the "latest" objects.
139+
140+
Takes the following optional arguments:
141+
142+
======================= =================================================
143+
Argument Description
144+
======================= =================================================
145+
``num_latest`` The number of items to display on the page.
146+
Defaults to 15.
147+
148+
``allow_empty`` **New in Django development version.**
149+
If ``False`` and there are no objects to display,
150+
the view will raise a 404 instead of displaying
151+
an empty index page. ``False`` is default.
152+
======================= =================================================
141153

142154
Uses the template ``app_label/module_name_archive`` by default.
143155

0 commit comments

Comments
 (0)