2011-02-16 2 views
0

월 및 연도별로 일반보기 아카이브 페이지를 만들려고합니다. 이처럼 :Django 년/월 일반보기가있는 아카이브 페이지

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 %} 

답변

3

doc에 따르면, archive_index 만 년으로 계산합니다. 당신은 년/월 그룹을 작성 할 수 있습니다 :

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}, 
    ) 

귀하의 템플릿 :

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

Y m은 날짜 객체를, 당신은 당신의 URL을 구성하는 모든 날짜 형식의 정보를 추출 할 수 있어야한다.

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> 
별로 그룹화