0

月別、年別の一般的なビューのアーカイブ ページを作成しようとしています。このような:

2011 - January March
2010 - October December

私が得ているもの:

2011 - January January
2010 - January January

これは可能ですか?ビューとテンプレートは次のとおりです。

見る

def track_archive(request):
    return date_based.archive_index(
        request,
        date_field='date',
        queryset=Track.objects.all(),
  )
track_archive.__doc__ = date_based.archive_index.__doc__

template
{% for year in date_list %}
        <a href="{% url track_archive %}{{ year|date:"Y" }}/">{{ year|date:"Y" }}</a> archives:
        {% for month in date_list %}
            <a href="{% url track_archive %}{{ year|date:"Y" }}/{{ month|date:"b" }}/">{{ month|date:"F" }}</a>
        {% endfor %}
    {% endfor %}
4

2 に答える 2

4

According to the doc, archive_index only calculates the years. You might want to write the year/month grouping:

def track_archive(request):
   tracks = Track.objects.all()
   archive = {}

   date_field = 'date'

   years = tracks.dates(date_field, 'year')[::-1]
   for date_year in years:
       months = tracks.filter(date__year=date_year.year).dates(date_field, 'month')
       archive[date_year] = months

   archive = sorted(archive.items(), reverse=True)

   return date_based.archive_index(
        request,
        date_field=date_field,
        queryset=tracks,
        extra_context={'archive': archive},
   )

Your template:

{% for y, months in archive %}
<div>
  {{ y.year }} archives: 
  {% for m in months %}
    {{ m|date:"F" }}
  {% endfor %}
</div>
{% endfor %}

y and m are date objects, you should be able to extract any date format information to construct your urls.

于 2011-02-16T21:50:44.237 に答える
4

クラスベースのジェネリックビューを使用する場合は、それを実行してジェネリックビューに固執することができます。

ArchiveIndexView を使用する代わりに、次のようなものを使用します

class IndexView(ArchiveIndexView):
    template_name="index.html"
    model = Article
    date_field="created"

    def get_context_data(self, **kwargs):
        context = super(IndexView,self).get_context_data(**kwargs)
        months = Article.objects.dates('created','month')[::-1]

        context['months'] = months
        return context

次に、テンプレートで、年ごとにグループ化できる月の辞書を取得します::

 <ul>
    {% for year, months in years.items %}
     <li> <a href ="{% url archive_year year %}"> {{ year }} <ul>
        {% for month in months %}
            <li> <a href ="{% url archive_month year month.month %}/">{{ month|date:"M Y" }}</a> </li>
        {% endfor %}
        </ul>
     </li>
    {% endfor %}
 </ul>
于 2012-05-03T06:13:24.903 に答える