Index: django/views/generic/date_based.py
===================================================================
--- django/views/generic/date_based.py (revision 4608)
+++ django/views/generic/date_based.py (working copy)
@@ -106,11 +106,15 @@
month:
(date) this month
next_month:
- (date) the first day of the next month, or None if the next month is in the future
+ (date) the first day of the next month, or None if the next month is in the future and allow_future is False
previous_month:
(date) the first day of the previous month
object_list:
list of objects published in the given month
+ next_object_date:
+ (date) the date for the first object after the beginning of the next month, or None if there is no such object or if the date is in the future and allow_future is False
+ previous_object_date:
+ (date) the date for the last object before the beginning of the this month, or None if there is no such object
"""
if extra_context is None: extra_context = {}
try:
@@ -144,6 +148,25 @@
else:
next_month = None
+ # Get the closest past object's date, if it exists.
+ lookup_kwargs = {'%s__lt' % date_field: first_day}
+ previous_list = queryset.filter(**lookup_kwargs).order_by('-%s' % date_field)
+ if previous_list:
+ previous_object_date = getattr(previous_list[0], date_field).date()
+ else:
+ previous_object_date = None
+
+ # Get the closest future object's date, if it exists and is allowed
+ if last_day >= now.date() and not allow_future:
+ next_object_date = None
+ else:
+ lookup_kwargs = {'%s__gte' % date_field: last_day}
+ next_list = queryset.filter(**lookup_kwargs).order_by(date_field)
+ if next_list:
+ next_object_date = getattr(next_list[0], date_field).date()
+ else:
+ next_object_date = None
+
if not template_name:
template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower())
t = template_loader.get_template(template_name)
@@ -152,6 +175,8 @@
'month': date,
'next_month': next_month,
'previous_month': first_day - datetime.timedelta(days=1),
+ 'next_%s_date' % template_object_name: next_object_date,
+ 'previous_%s_date' % template_object_name: previous_object_date,
}, context_processors)
for key, value in extra_context.items():
if callable(value):
@@ -225,7 +250,11 @@
previous_day
(datetime) the previous 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 and allow_future is False
+ next_object_date:
+ (date) the date for the first object after the beginning of the next month, or None if there is no such object or if the date is in the future and allow_future is False
+ previous_object_date:
+ (date) the date for the last object before the beginning of the this month, or None if there is no such object
"""
if extra_context is None: extra_context = {}
try:
@@ -256,6 +285,32 @@
else:
next_day = None
+ # Get the closest past object's date, if it exists.
+ if isinstance(model._meta.get_field(date_field), DateTimeField):
+ lookup_kwargs = {'%s__lt' % date_field: datetime.datetime.combine(date, datetime.time.min)}
+ else:
+ lookup_kwargs = {'%s__lt' % date_field: date}
+ previous_list = queryset.filter(**lookup_kwargs).order_by('-%s' % date_field)
+ if previous_list:
+ previous_object_date = getattr(previous_list[0], date_field).date()
+ else:
+ previous_object_date = None
+
+ # Get the closest future object's date, if it exists and is allowed
+ if date >= now.date() and not allow_future:
+ next_object_date = None
+ else:
+ if isinstance(model._meta.get_field(date_field), DateTimeField):
+ lookup_kwargs = {'%s__gt' % date_field: datetime.datetime.combine(date, datetime.time.min)}
+ else:
+ lookup_kwargs = {'%s__gt' % date_field: date}
+ next_list = queryset.filter(**lookup_kwargs).order_by(date_field)
+ if next_list:
+ next_object_date = getattr(next_list[0], date_field).date()
+ else:
+ next_object_date = None
+
+
if not template_name:
template_name = "%s/%s_archive_day.html" % (model._meta.app_label, model._meta.object_name.lower())
t = template_loader.get_template(template_name)
@@ -264,6 +319,8 @@
'day': date,
'previous_day': date - datetime.timedelta(days=1),
'next_day': next_day,
+ 'next_%s_date' % template_object_name: next_object_date,
+ 'previous_%s_date' % template_object_name: previous_object_date,
}, context_processors)
for key, value in extra_context.items():
if callable(value):
Index: docs/generic_views.txt
===================================================================
--- docs/generic_views.txt (revision 4608)
+++ docs/generic_views.txt (working copy)
@@ -384,18 +384,36 @@
* ``month``: A ``datetime.date`` object representing the given month.
* ``next_month``: A ``datetime.date`` object representing the first day of
- the next month. If the next month is in the future, this will be
- ``None``.
+ the next month. If the next month is in the future and ``allow_future`` is
+ ``False``, this will be ``None``.
* ``previous_month``: A ``datetime.date`` object representing the first day
of the previous month. Unlike ``next_month``, this will never be
``None``.
+ * ``previous_month``: A ``datetime.date`` object representing the day of the
+ last post of the previous month. If there is no such post this will be
+ ``None``.
+
* ``object_list``: A list of objects available for the given month. This
variable's name depends on the ``template_object_name`` parameter, which
is ``'object'`` by default. If ``template_object_name`` is ``'foo'``,
this variable's name will be ``foo_list``.
+ * ``next_object_date``: A ``datetime.date`` object representing the date of the
+ first object dated after the start of next month. If there is no such object
+ or if the date is in the future and ``allow_future`` is ``False``, this will
+ be ``None``. This variable's name depends on the ``template_object_name``
+ parameter, which is ``'object'`` by default. If ``template_object_name`` is
+ ``'foo'``, this variable's name will be ``next_foo_date``.
+
+ * ``previous_object_date``: A ``datetime.date`` object representing the date of
+ the last object dated before the start of the given month. If there is no such
+ object, this will be ``None``. This variable's name depends on the
+ ``template_object_name`` parameter, which is ``'object'`` by default.
+ If ``template_object_name`` is ``'foo'``, this variable's name will be
+ ``previous_foo_date``.
+
.. _strftime docs: http://www.python.org/doc/current/lib/module-time.html#l2h-1941
``django.views.generic.date_based.archive_week``
@@ -550,7 +568,8 @@
* ``day``: A ``datetime.date`` object representing the given day.
* ``next_day``: A ``datetime.date`` object representing the next day. If
- the next day is in the future, this will be ``None``.
+ the next day is in the future, and ``allow_future`` is ``False``, this
+ will be ``None``.
* ``previous_day``: A ``datetime.date`` object representing the given day.
Unlike ``next_day``, this will never be ``None``.
@@ -560,6 +579,20 @@
is ``'object'`` by default. If ``template_object_name`` is ``'foo'``,
this variable's name will be ``foo_list``.
+ * ``next_object_date``: A ``datetime.date`` object representing the date of the
+ first object dated after the given day. If there is no such object or if the
+ date is in the future and ``allow_future`` is ``False``, this will be ``None``.
+ This variable's name depends on the ``template_object_name`` parameter, which
+ is ``'object'`` by default. If ``template_object_name`` is ``'foo'``,
+ this variable's name will be ``next_foo_date``.
+
+ * ``previous_object_date``: A ``datetime.date`` object representing the date of
+ the last object dated before the given day. If there is no such object, this
+ will be ``None``. This variable's name depends on the ``template_object_name``
+ parameter, which is ``'object'`` by default. If ``template_object_name`` is
+ ``'foo'``, this variable's name will be ``previous_foo_date``.
+
+
``django.views.generic.date_based.archive_today``
-------------------------------------------------