I am using Django's generic views to create a blog site. The
templates I created, entry_archive_day, entry_archive_month,
entry_archive, and entry_detail all work perfectly.
But entry_archive_year does not. Instead, it is simply a valid page with no content (not a 404 or other error. It looks like it sees no objects in **object_list**.
I know that archive uses a latest list instead of object_list,
but that's not the case with archive_year, correct?
Thanks!
To solve your problem:
If you set make_object_list=True when calling archive_year, then the list of objects for that year will be available as object_list.
As a quick example, if your url pattern looks like
url(r'^(?P<year>\d{4})/$', 'archive_year', info_dict, name="entry_archive_year")
where info_dict is a dictionary containing the queryset and date_field, change it to
url(r'^(?P<year>\d{4}/$', 'archive_year', dict(info_dict,make_object_list=True),
name="entry_archive_year")
Explanation:
The generic view archive_year has an optional argument make_object_list. By default, it is set to false, and object_list is passed to the template as an empty list.
make_object_list: A boolean specifying whether to retrieve the full list of objects for this year and pass those to the template. IfTrue, this list of objects will be made available to the template asobject_list. (The nameobject_listmay be different; see the docs forobject_listin the "Template context" section below.) By default, this isFalse.
A reason for this is that you might not always want to display the entire object list in the entry_archive_year view. You may have hundreds of blog posts for that year, too many to display on one page.
Instead, archive_year adds date_list to the template context. This allows you to create links to the monthly archive pages of that year, for the months which have entries.
date_list: A list ofdatetime.dateobjects representing all months that have objects available in the given year, according to queryset, in ascending order.
There's more info in the Django docs.
As requested in the comment below, an example of how to use date_list:
To use date_list, your entry_archive_year template would contain something like this:
<ul>
{% for month in date_list %}
<li><a href="/blog/{{month|date:"Y"}}/{{month|date:"b"}}>
{{month|date:"F"}}</a></li>
{% endfor %}
</ul>
Note that I've hardcoded the url - in practice it would be better to use the url template tag. For an example of date_list being used in the wild, look at the Django Weblog 2009 Archive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With