Removed 'use_numeric_months' parameter in generic date views (from [308]) 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
This commit is contained in:
Adrian Holovaty 2005-07-26 03:48:41 +00:00
parent 48441f467f
commit 7d574e8f76
2 changed files with 47 additions and 52 deletions

View File

@ -85,8 +85,8 @@ def archive_year(request, year, app_label, module_name, date_field,
return HttpResponse(t.render(c)) return HttpResponse(t.render(c))
def archive_month(request, year, month, app_label, module_name, date_field, def archive_month(request, year, month, app_label, module_name, date_field,
use_numeric_months=False, template_name=None, month_format='%b', template_name=None, extra_lookup_kwargs={},
extra_lookup_kwargs={}, extra_context={}): extra_context={}):
""" """
Generic monthly archive view. Generic monthly archive view.
@ -97,16 +97,10 @@ def archive_month(request, year, month, app_label, module_name, date_field,
object_list: object_list:
list of objects published in the given month list of objects published in the given month
""" """
if use_numeric_months: try:
try: date = datetime.date(*time.strptime(year+month, '%Y'+month_format)[:3])
date = datetime.date(int(year), int(month), 1) except ValueError:
except (ValueError, TypeError): raise Http404
raise Http404
else:
try:
date = datetime.date(*time.strptime(year+month, '%Y%b')[:3])
except ValueError:
raise Http404
mod = get_module(app_label, module_name) mod = get_module(app_label, module_name)
now = datetime.datetime.now() now = datetime.datetime.now()
@ -143,8 +137,8 @@ def archive_month(request, year, month, app_label, module_name, date_field,
return HttpResponse(t.render(c)) return HttpResponse(t.render(c))
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,
use_numeric_months=False, template_name=None, extra_lookup_kwargs={}, month_format='%b', day_format='%d', template_name=None,
extra_context={}, allow_empty=False): extra_lookup_kwargs={}, extra_context={}, allow_empty=False):
""" """
Generic daily archive view. Generic daily archive view.
@ -159,16 +153,10 @@ def archive_day(request, year, month, day, app_label, module_name, date_field,
next_day next_day
(datetime) the next day, or None if the current day is today (datetime) the next day, or None if the current day is today
""" """
if use_numeric_months: try:
try: date = datetime.date(*time.strptime(year+month+day, '%Y'+month_format+day_format)[:3])
date = datetime.date(int(year), int(month), int(day)) except ValueError:
except (ValueError, TypeError): raise Http404
raise Http404
else:
try:
date = datetime.date(*time.strptime(year+month+day, '%Y%b%d')[:3])
except ValueError:
raise Http404
mod = get_module(app_label, module_name) mod = get_module(app_label, module_name)
now = datetime.datetime.now() now = datetime.datetime.now()
@ -211,9 +199,9 @@ def archive_today(request, **kwargs):
return archive_day(request, **kwargs) return archive_day(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,
use_numeric_months=False, object_id=None, slug=None, slug_field=None, month_format='%b', day_format='%d', object_id=None, slug=None,
template_name=None, template_name_field=None, extra_lookup_kwargs={}, slug_field=None, template_name=None, template_name_field=None,
extra_context={}): extra_lookup_kwargs={}, 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.
@ -222,16 +210,10 @@ def object_detail(request, year, month, day, app_label, module_name, date_field,
object: object:
the object to be detailed the object to be detailed
""" """
if use_numeric_months: try:
try: date = datetime.date(*time.strptime(year+month+day, '%Y'+month_format+day_format)[:3])
date = datetime.date(int(year), int(month), int(day)) except ValueError:
except (ValueError, TypeError): raise Http404
raise Http404
else:
try:
date = datetime.date(*time.strptime(year+month+day, '%Y%b%d')[:3])
except ValueError:
raise Http404
mod = get_module(app_label, module_name) mod = get_module(app_label, module_name)
now = datetime.datetime.now() now = datetime.datetime.now()

View File

@ -126,9 +126,13 @@ The date-based generic functions are:
``archive_month`` ``archive_month``
Monthly archive. Requires that ``year`` and ``month`` arguments be given. Monthly archive. Requires that ``year`` and ``month`` arguments be given.
You may pass the additional option ``use_numeric_months`` if you'd like to You can pass the additional option ``month_format`` if you'd like to change
use URLs that use numbers instead of names for months (i.e. ``/2005/01/15/`` the way months are specified in the URL.
instead of ``/2005/jan/15/``.
``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. Uses the template ``app_label/module_name__archive_month`` by default.
@ -143,6 +147,10 @@ The date-based generic functions are:
Daily archive. Requires that ``year``, ``month``, and ``day`` arguments be Daily archive. Requires that ``year``, ``month``, and ``day`` arguments be
given. 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. Uses the template ``app_label/module_name__archive_day`` by default.
Has the following template context: 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 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. 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 Using list/detail generic views
=============================== ===============================