Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Removed 'use_numeric_months' parameter in generic date views (from [3…

…08]) in favor of something more powerful -- you can now provide month_format and day_format, which are format strings that specify how you expect the month and day to be formatted in the URL.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@312 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 7d574e8f7647a1ac8e2613c4be9c45adf4b5fc0e 1 parent 48441f4
Adrian Holovaty adrianholovaty authored
Showing with 47 additions and 52 deletions.
  1. +30 −48 django/views/generic/date_based.py
  2. +17 −4 docs/generic_views.txt
78 django/views/generic/date_based.py
View
@@ -6,7 +6,7 @@
from django.utils.httpwrappers import HttpResponse
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, extra_lookup_kwargs={}, extra_context={}):
"""
Generic top-level archive of date-based objects.
@@ -44,11 +44,11 @@ def archive_index(request, app_label, module_name, date_field, num_latest=15,
for key, value in extra_context.items():
if callable(value):
c[key] = value()
- else:
+ else:
c[key] = value
return HttpResponse(t.render(c))
-def archive_year(request, year, app_label, module_name, date_field,
+def archive_year(request, year, app_label, module_name, date_field,
template_name=None, extra_lookup_kwargs={}, extra_context={}):
"""
Generic yearly archive view.
@@ -80,13 +80,13 @@ def archive_year(request, year, app_label, module_name, date_field,
for key, value in extra_context.items():
if callable(value):
c[key] = value()
- else:
+ else:
c[key] = value
return HttpResponse(t.render(c))
-def archive_month(request, year, month, app_label, module_name, date_field,
- use_numeric_months=False, template_name=None,
- extra_lookup_kwargs={}, extra_context={}):
+def archive_month(request, year, month, app_label, module_name, date_field,
+ month_format='%b', template_name=None, extra_lookup_kwargs={},
+ extra_context={}):
"""
Generic monthly archive view.
@@ -97,17 +97,11 @@ def archive_month(request, year, month, app_label, module_name, date_field,
object_list:
list of objects published in the given month
"""
- if use_numeric_months:
- try:
- date = datetime.date(int(year), int(month), 1)
- except (ValueError, TypeError):
- raise Http404
- else:
- try:
- date = datetime.date(*time.strptime(year+month, '%Y%b')[:3])
- except ValueError:
- raise Http404
-
+ try:
+ date = datetime.date(*time.strptime(year+month, '%Y'+month_format)[:3])
+ except ValueError:
+ raise Http404
+
mod = get_module(app_label, module_name)
now = datetime.datetime.now()
# Calculate first and last day of month, for use in a date-range lookup.
@@ -138,13 +132,13 @@ def archive_month(request, year, month, app_label, module_name, date_field,
for key, value in extra_context.items():
if callable(value):
c[key] = value()
- else:
+ else:
c[key] = value
return HttpResponse(t.render(c))
-def archive_day(request, year, month, day, app_label, module_name, date_field,
- use_numeric_months=False, template_name=None, extra_lookup_kwargs={},
- extra_context={}, allow_empty=False):
+def archive_day(request, year, month, day, app_label, module_name, date_field,
+ month_format='%b', day_format='%d', template_name=None,
+ extra_lookup_kwargs={}, extra_context={}, allow_empty=False):
"""
Generic daily archive view.
@@ -159,16 +153,10 @@ def archive_day(request, year, month, day, app_label, module_name, date_field,
next_day
(datetime) the next day, or None if the current day is today
"""
- if use_numeric_months:
- try:
- date = datetime.date(int(year), int(month), int(day))
- except (ValueError, TypeError):
- raise Http404
- else:
- try:
- date = datetime.date(*time.strptime(year+month+day, '%Y%b%d')[:3])
- except ValueError:
- raise Http404
+ try:
+ date = datetime.date(*time.strptime(year+month+day, '%Y'+month_format+day_format)[:3])
+ except ValueError:
+ raise Http404
mod = get_module(app_label, module_name)
now = datetime.datetime.now()
@@ -194,7 +182,7 @@ def archive_day(request, year, month, day, app_label, module_name, date_field,
for key, value in extra_context.items():
if callable(value):
c[key] = value()
- else:
+ else:
c[key] = value
return HttpResponse(t.render(c))
@@ -210,10 +198,10 @@ def archive_today(request, **kwargs):
})
return archive_day(request, **kwargs)
-def object_detail(request, year, month, day, app_label, module_name, date_field,
- use_numeric_months=False, object_id=None, slug=None, slug_field=None,
- template_name=None, template_name_field=None, extra_lookup_kwargs={},
- extra_context={}):
+def object_detail(request, year, month, day, app_label, module_name, date_field,
+ month_format='%b', day_format='%d', object_id=None, slug=None,
+ slug_field=None, template_name=None, template_name_field=None,
+ extra_lookup_kwargs={}, extra_context={}):
"""
Generic detail view from year/month/day/slug or year/month/day/id structure.
@@ -222,16 +210,10 @@ def object_detail(request, year, month, day, app_label, module_name, date_field,
object:
the object to be detailed
"""
- if use_numeric_months:
- try:
- date = datetime.date(int(year), int(month), int(day))
- except (ValueError, TypeError):
- raise Http404
- else:
- try:
- date = datetime.date(*time.strptime(year+month+day, '%Y%b%d')[:3])
- except ValueError:
- raise Http404
+ try:
+ date = datetime.date(*time.strptime(year+month+day, '%Y'+month_format+day_format)[:3])
+ except ValueError:
+ raise Http404
mod = get_module(app_label, module_name)
now = datetime.datetime.now()
@@ -265,7 +247,7 @@ def object_detail(request, year, month, day, app_label, module_name, date_field,
for key, value in extra_context.items():
if callable(value):
c[key] = value()
- else:
+ else:
c[key] = value
response = HttpResponse(t.render(c))
populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name))
21 docs/generic_views.txt
View
@@ -126,10 +126,14 @@ The date-based generic functions are:
``archive_month``
Monthly archive. Requires that ``year`` and ``month`` arguments be given.
- You may pass the additional option ``use_numeric_months`` if you'd like to
- use URLs that use numbers instead of names for months (i.e. ``/2005/01/15/``
- instead of ``/2005/jan/15/``.
-
+ You can pass the additional option ``month_format`` if you'd like to change
+ the way months are specified in the URL.
+
+ ``month_format`` is a format string in the same syntax accepted by Python's
+ ``time.strftime``. (See the `strftime docs`_.) It's set to ``"%b"`` by
+ default, which is a three-letter month abbreviation. To change it to use
+ numbers, use ``"%m"``.
+
Uses the template ``app_label/module_name__archive_month`` by default.
Has the following template context:
@@ -143,6 +147,10 @@ The date-based generic functions are:
Daily archive. Requires that ``year``, ``month``, and ``day`` arguments be
given.
+ As in ``archive_month``, you can pass an optional ``month_format``. You can
+ also pass ``day_format``, which defaults to ``"%d"`` (day of the month as a
+ decimal number, 1-31).
+
Uses the template ``app_label/module_name__archive_day`` by default.
Has the following template context:
@@ -177,6 +185,11 @@ The date-based generic functions are:
You can also pass the ``template_name_field`` argument to indicate that the
the object stores the name of its template in a field on the object itself.
+ As in ``archive_day``, ``object_detail`` takes optional ``month_format``
+ and ``day_format`` parameters.
+
+.. _strftime docs: http://www.python.org/doc/current/lib/module-time.html#l2h-1941
+
Using list/detail generic views
===============================
Please sign in to comment.
Something went wrong with that request. Please try again.