Generic date ビューLink to this heading

日付ベースの汎用ビュー (django.views.generic.dates 内で提供されています) は、日付ベースのデータのために、ドリルダウンページを描画するためのビューです。

ArchiveIndexViewLink to this heading

class ArchiveIndexViewLink to this definition

"最新の"オブジェクトを、日付によって表示するトップレベルのインデックスページです。allow_futureTrue にセットしない限り、未来 の日付のオブジェクトは含まれません。

継承元 (MRO)

コンテキスト

(django.views.generic.dates.BaseDateListView を通じた) django.views.generic.list.MultipleObjectMixin によって提供されたコンテキストに加えて、テンプレートのコンテキストは以下のようになります:

  • date_list: queryset に従ってオブジェクトを利用可能にするすべての年を含む QuerySet オブジェクトです。datetime.datetime オブジェクトとして表され、降順で表示されます。

ノート

  • デフォルトの 最新context_object_name を使います。

  • デフォルトの _archivetemplate_name_suffix を使います。

  • 年によって date_list を提供する動作がデフォルトですが、date_list_period 属性を用いた月や日に変更できます。これは、すべてのサブクラスのビューに適用されます。

例 myapp/urls.py:

Code
from django.conf.urls import url
from django.views.generic.dates import ArchiveIndexView

from myapp.models import Article

urlpatterns = [
    url(r'^archive/$',
        ArchiveIndexView.as_view(model=Article, date_field="pub_date"),
        name="article_archive"),
]

例 myapp/article_archive.html:

Django template
<ul>
    {% for article in latest %}
        <li>{{ article.pub_date }}: {{ article.title }}</li>
    {% endfor %}
</ul>

これは、すべての記事 (article) を出力します。

YearArchiveViewLink to this heading

class YearArchiveViewLink to this definition

与えられた年の中で有効な月を表示する、年次のアーカイブページです。allow_futureTrue にセットしない限り、未来 の日付を伴うオブジェクトは表示されません。

継承元 (MRO)

make_object_listLink to this definition

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. If False, the None queryset will be used as the object list. By default, this is False.

get_make_object_list()Link to this definition

Determine if an object list will be returned as part of the context. Returns make_object_list by default.

コンテキスト

(django.views.generic.dates.BaseDateListView を通じた) django.views.generic.list.MultipleObjectMixin によって提供されたコンテキストに加えて、テンプレートのコンテキストは以下のようになります:

  • date_list: A QuerySet object containing all months that have objects available according to queryset, represented as datetime.datetime objects, in ascending order.

  • year: A date object representing the given year.

  • next_year: A date object representing the first day of the next year, according to allow_empty and allow_future.

  • previous_year: A date object representing the first day of the previous year, according to allow_empty and allow_future.

ノート

  • Uses a default template_name_suffix of _archive_year.

Example myapp/views.py:

Code
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

例 myapp/urls.py:

Code
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:

Django template
<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>

MonthArchiveViewLink to this heading

class MonthArchiveViewLink to this definition

与えられた月の中で有効な全オブジェクトを表示する、月次のアーカイブページです。allow_futureTrue にセットしない限り、未来 の日付を伴うオブジェクトは表示されません。

継承元 (MRO)

コンテキスト

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

  • date_list: A QuerySet object containing all days that have objects available in the given month, according to queryset, represented as datetime.datetime objects, in ascending order.

  • month: A date object representing the given month.

  • next_month: A date object representing the first day of the next month, according to allow_empty and allow_future.

  • previous_month: A date object representing the first day of the previous month, according to allow_empty and allow_future.

ノート

  • Uses a default template_name_suffix of _archive_month.

Example myapp/views.py:

Code
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

例 myapp/urls.py:

Code
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:

Django template
<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>

WeekArchiveViewLink to this heading

class WeekArchiveViewLink to this definition

与えられた週の中で有効な全オブジェクトを表示する、週次のアーカイブページです。allow_futureTrue にセットしない限り、未来 の日付を伴うオブジェクトは表示されません。

継承元 (MRO)

コンテキスト

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

  • week: A date object representing the first day of the given week.

  • next_week: A date object representing the first day of the next week, according to allow_empty and allow_future.

  • previous_week: A date object representing the first day of the previous week, according to allow_empty and allow_future.

ノート

  • Uses a default template_name_suffix of _archive_week.

  • The week_format attribute is a strptime() format string used to parse the week number. The following values are supported:

    • '%U': Based on the United States week system where the week begins on Sunday. This is the default value.

    • '%W': Similar to '%U', except it assumes that the week begins on Monday. This is not the same as the ISO 8601 week number.

Example myapp/views.py:

Code
from django.views.generic.dates import WeekArchiveView

from myapp.models import Article

class ArticleWeekArchiveView(WeekArchiveView):
    queryset = Article.objects.all()
    date_field = "pub_date"
    week_format = "%W"
    allow_future = True

例 myapp/urls.py:

Code
from django.conf.urls import url

from myapp.views import ArticleWeekArchiveView

urlpatterns = [
    # Example: /2012/week/23/
    url(r'^(?P<year>[0-9]{4})/week/(?P<week>[0-9]+)/$',
        ArticleWeekArchiveView.as_view(),
        name="archive_week"),
]

Example myapp/article_archive_week.html:

Django template
<h1>Week {{ week|date:'W' }}</h1>

<ul>
    {% for article in object_list %}
        <li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
    {% endfor %}
</ul>

<p>
    {% if previous_week %}
        Previous Week: {{ previous_week|date:"W" }} of year {{ previous_week|date:"Y" }}
    {% endif %}
    {% if previous_week and next_week %}--{% endif %}
    {% if next_week %}
        Next week: {{ next_week|date:"W" }} of year {{ next_week|date:"Y" }}
    {% endif %}
</p>

In this example, you are outputting the week number. Keep in mind that week numbers computed by the date template filter with the 'W' format character are not always the same as those computed by strftime() and strptime() with the '%W' format string. For year 2015, for example, week numbers output by date are higher by one compared to those output by strftime(). There isn't an equivalent for the '%U' strftime() format string in date. Therefore, you should avoid using date to generate URLs for WeekArchiveView.

DayArchiveViewLink to this heading

class DayArchiveViewLink to this definition

与えられた日の中で有効な全オブジェクトを表示する、日次のアーカイブページです。allow_futureTrue にセットしない限り、未来の日付に対するオブジェクトがするかどうかにかかわらず、未来の日付は 404 エラーを返します。

継承元 (MRO)

コンテキスト

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

ノート

  • Uses a default template_name_suffix of _archive_day.

Example myapp/views.py:

Code
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

例 myapp/urls.py:

Code
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:

Django template
<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>

TodayArchiveViewLink to this heading

class TodayArchiveViewLink to this definition

今日 の全てのオブジェクトを表示する、日時のアーカイブページです。 / / 引数の代わりに今日の日付が使われること以外は、django.views.generic.dates.DayArchiveView とまったく同じです。

継承元 (MRO)

ノート

  • Uses a default template_name_suffix of _archive_today.

Example myapp/views.py:

Code
from django.views.generic.dates import TodayArchiveView

from myapp.models import Article

class ArticleTodayArchiveView(TodayArchiveView):
    queryset = Article.objects.all()
    date_field = "pub_date"
    allow_future = True

例 myapp/urls.py:

Code
from django.conf.urls import url

from myapp.views import ArticleTodayArchiveView

urlpatterns = [
    url(r'^today/$',
        ArticleTodayArchiveView.as_view(),
        name="archive_today"),
]

DateDetailViewLink to this heading

class DateDetailViewLink to this definition

個別のオブジェクトを表示するページです。オブジェクトが未来の日付値を持っている場合、allow_futureTrue にセットしない限り、デフォルトでビューは 404 エラーを返します。

継承元 (MRO)

コンテキスト

  • Includes the single object associated with the model specified in the DateDetailView.

ノート

  • Uses a default template_name_suffix of _detail.

例 myapp/urls.py:

Code
from django.conf.urls import url
from django.views.generic.dates import DateDetailView

urlpatterns = [
    url(r'^(?P<year>[0-9]{4})/(?P<month>[-\w]+)/(?P<day>[0-9]+)/(?P<pk>[0-9]+)/$',
        DateDetailView.as_view(model=Article, date_field="pub_date"),
        name="archive_date_detail"),
]

Example myapp/article_detail.html:

Django template
<h1>{{ object.title }}</h1>