From Classy Class Based Views ArchiveIndexView
Top-level archive of date-based items.
Attributes
There are 20 attributes that can be set for the ArchiveIndexView
but most of them are based on ancestral Classes of the CBV so we won’t be going into them in Detail.
DateMixin Attributes
- allow_future: Defaults to False. If set to True you can show items that have dates that are in the future where the future is anything after the current date/time on the server.
- date_field: the field that the view will use to filter the date on. If this is not set an error will be generated
- uses_datetime_field: Convert a date into a datetime when the date field is a DateTimeField. When time zone support is enabled,
date
is assumed to be in the current time zone, so that displayed items are consistent with the URL.
BaseDateListView Attributes
- allow_empty: Defaults to
False
. This means that if there is no data a404
error will be returned with the message
No __str__ Available
where ‘__str__
’ is the display of your model - date_list_period: This attribute allows you to break down by a specific period of time (years, months, days, etc.) and group your date driven items by the period specified. See below for implementation
For year
views.py
date_list_period='year'
urls.py
Nothing special needs to be done
<file_name_>.html
{% block content %}
<div>
{% for date in date_list %}
{{ date.year }}
<ul>
{% for p in person %}
{% if date.year == p.post_date.year %}
<li>{{ p.post_date }}: {{ p.first_name }} {{ p.last_name }}</li>
{% endif %}
{% endfor %}
</ul>
{% endfor %}
</div>
{% endblock %}
Will render:
For month
views.py
date_list_period='month'
urls.py
Nothing special needs to be done
<file_name_>.html
{% block content %}
<div>
{% for date in date_list %}
{{ date.month }}
<ul>
{% for p in person %}
{% if date.month == p.post_date.month %}
<li>{{ p.post_date }}: {{ p.first_name }} {{ p.last_name }}</li>
{% endif %}
{% endfor %}
</ul>
{% endfor %}
</div>
{% endblock %}
Will render:
BaseArchiveIndexView Attributes
- context_object_name: Name the object used in the template. As stated before, you’re going to want to do this so you don’t hate yourself (or have other developers hate you).
Other Attributes
MultipleObjectMixin Attributes
These attributes were all reviewed in the ListView post
- model = None
- ordering = None
- page_kwarg = ‘page’
- paginate_by = None
- paginate_orphans = 0
- paginator_class = <class ‘django.core.paginator.Paginator’>
- queryset = None
TemplateResponseMixin Attributes
This attribute was reviewed in the ListView post
- content_type = None
ContextMixin Attributes
This attribute was reviewed in the ListView post
- extra_context = None
View Attributes
This attribute was reviewed in the View post
- http_method_names = [‘get’, ‘post’, ‘put’, ‘patch’, ‘delete’, ‘head’, ‘options’, ‘trace’]
TemplateResponseMixin Attributes
These attributes were all reviewed in the ListView post
- response_class = <class ‘django.template.response.TemplateResponse’>
- template_engine = None
- template_name = None
Diagram
A visual representation of how ArchiveIndexView
is derived can be seen here:
Conclusion
With date driven data (articles, blogs, etc.) The ArchiveIndexView
is a great CBV and super easy to implement.