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.MultipleObjectTemplateResponseMixindjango.views.generic.base.TemplateResponseMixindjango.views.generic.dates.BaseMonthArchiveViewdjango.views.generic.dates.YearMixindjango.views.generic.dates.MonthMixindjango.views.generic.dates.BaseDateListViewdjango.views.generic.list.MultipleObjectMixindjango.views.generic.dates.DateMixindjango.views.generic.base.View
Context
In addition to the context provided by MultipleObjectMixin (via BaseDateListView), the template’s context will be:
-
date_list: AQuerySetobject containing all days that have objects available in the given month, according toqueryset, represented asdatetime.datetimeobjects, in ascending order. -
month: Adateobject representing the given month. -
next_month: Adateobject representing the first day of the next month, according toallow_emptyandallow_future. -
previous_month: Adateobject representing the first day of the previous month, according toallow_emptyandallow_future.
Notes
- Uses a default
template_name_suffixof_archive_month.
Example myapp/views.py:
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:
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:
<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.