class YearArchiveView [source]
A yearly archive page showing all available months in a given year. 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.BaseYearArchiveViewdjango.views.generic.dates.YearMixindjango.views.generic.dates.BaseDateListViewdjango.views.generic.list.MultipleObjectMixindjango.views.generic.dates.DateMixindjango.views.generic.base.View
-
make_object_list -
A boolean specifying whether to retrieve the full list of objects for this year and pass those to the template. If
True, the list of objects will be made available to the context. IfFalse, theNonequeryset will be used as the object list. By default, this isFalse.
-
get_make_object_list() -
Determine if an object list will be returned as part of the context. Returns
make_object_listby default.
Context
In addition to the context provided by django.views.generic.list.MultipleObjectMixin (via django.views.generic.dates.BaseDateListView), the template’s context will be:
-
date_list: AQuerySetobject containing all months that have objects available according toqueryset, represented asdatetime.datetimeobjects, in ascending order. -
year: Adateobject representing the given year. -
next_year: Adateobject representing the first day of the next year, according toallow_emptyandallow_future. -
previous_year: Adateobject representing the first day of the previous year, according toallow_emptyandallow_future.
Notes
- Uses a default
template_name_suffixof_archive_year.
Example myapp/views.py:
from django.views.generic.dates import YearArchiveView
from myapp.models import Article
class ArticleYearArchiveView(YearArchiveView):
queryset = Article.objects.all()
date_field = "pub_date"
make_object_list = True
allow_future = True
Example myapp/urls.py:
from django.conf.urls import url
from myapp.views import ArticleYearArchiveView
urlpatterns = [
url(r'^(?P<year>[0-9]{4})/$',
ArticleYearArchiveView.as_view(),
name="article_year_archive"),
]
Example myapp/article_archive_year.html:
<ul>
{% for date in date_list %}
<li>{{ date|date }}</li>
{% endfor %}
</ul>
<div>
<h1>All Articles for {{ year|date:"Y" }}</h1>
{% for obj in object_list %}
<p>
{{ obj.title }} - {{ obj.pub_date|date:"F j, Y" }}
</p>
{% endfor %}
</div>
Please login to continue.