class MonthArchiveView
[source]
A monthly archive page showing all objects in a given month. Objects with a date in the future are not displayed unless you set allow_future
to True
.
Ancestors (MRO)
django.views.generic.list.MultipleObjectTemplateResponseMixin
django.views.generic.base.TemplateResponseMixin
django.views.generic.dates.BaseMonthArchiveView
django.views.generic.dates.YearMixin
django.views.generic.dates.MonthMixin
django.views.generic.dates.BaseDateListView
django.views.generic.list.MultipleObjectMixin
django.views.generic.dates.DateMixin
django.views.generic.base.View
Context
In addition to the context provided by MultipleObjectMixin
(via BaseDateListView
), the template’s context will be:
-
date_list
: AQuerySet
object containing all days that have objects available in the given month, according toqueryset
, represented asdatetime.datetime
objects, in ascending order. -
month
: Adate
object representing the given month. -
next_month
: Adate
object representing the first day of the next month, according toallow_empty
andallow_future
. -
previous_month
: Adate
object representing the first day of the previous month, according toallow_empty
andallow_future
.
Notes
- Uses a default
template_name_suffix
of_archive_month
.
Example myapp/views.py:
1 2 3 4 5 6 7 8 | from django.views.generic.dates import MonthArchiveView from myapp.models import Article class ArticleMonthArchiveView(MonthArchiveView): queryset = Article.objects. all () date_field = "pub_date" allow_future = True |
Example myapp/urls.py:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | from django.conf.urls import url from myapp.views import ArticleMonthArchiveView urlpatterns = [ # Example: /2012/aug/ url(r '^(?P<year>[0-9]{4})/(?P<month>[-\w]+)/$' , ArticleMonthArchiveView.as_view(), name = "archive_month" ), # Example: /2012/08/ url(r '^(?P<year>[0-9]{4})/(?P<month>[0-9]+)/$' , ArticleMonthArchiveView.as_view(month_format = '%m' ), name = "archive_month_numeric" ), ] |
Example myapp/article_archive_month.html:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | < ul > {% for article in object_list %} < li >{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</ li > {% endfor %} </ ul > < p > {% if previous_month %} Previous Month: {{ previous_month|date:"F Y" }} {% endif %} {% if next_month %} Next Month: {{ next_month|date:"F Y" }} {% endif %} </ p > |
Please login to continue.