views.generic.dates.DayArchiveView

class DayArchiveView [source]

A day archive page showing all objects in a given day. Days in the future throw a 404 error, regardless of whether any objects exist for future days, unless you set allow_future to True.

Ancestors (MRO)

Context

In addition to the context provided by MultipleObjectMixin (via BaseDateListView), the template’s context will be:

Notes

  • Uses a default template_name_suffix of _archive_day.

Example myapp/views.py:

1
2
3
4
5
6
7
8
from django.views.generic.dates import DayArchiveView
 
from myapp.models import Article
 
class ArticleDayArchiveView(DayArchiveView):
    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
from django.conf.urls import url
 
from myapp.views import ArticleDayArchiveView
 
urlpatterns = [
    # Example: /2012/nov/10/
    url(r'^(?P<year>[0-9]{4})/(?P<month>[-\w]+)/(?P<day>[0-9]+)/$',
        ArticleDayArchiveView.as_view(),
        name="archive_day"),
]

Example myapp/article_archive_day.html:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<h1>{{ day }}</h1>
 
<ul>
    {% for article in object_list %}
        <li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
    {% endfor %}
</ul>
 
<p>
    {% if previous_day %}
        Previous Day: {{ previous_day }}
    {% endif %}
    {% if previous_day and next_day %}--{% endif %}
    {% if next_day %}
        Next Day: {{ next_day }}
    {% endif %}
</p>
doc_Django
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.