2012-02-06 4 views
3

약간 궁금한 질문 일지 모르지만 나는 새로운 Date Based views in django을 어떻게 사용할 수 있는지 이해하려고 노력하고 있지만 예제가 없으면 막 다른 골목에 있습니다. 내가하고 싶은 일은 페이지에 내 블로그 항목을 모두 표시하는 것입니다 (페이지 매김 포함). 그리고 사이드 네비게이션에서 년 및 월에 따라 완료된 아카이브를 보여주고 싶습니다.Django에서 날짜 기반보기를 사용하는 방법

내가 원했던 것은 매우 기본적이고 아래 첨부 된 그림에서 볼 수 있습니다. 누군가가 나에게 예를 제공 할 수있는 경우

enter image description here

그때는 정말 좋은 것입니다. 템플릿을 처리 할 수 ​​있지만 클래스 기반 일반 뷰를 사용하는 방법을 알아야합니다. 제네릭보기가 있다면별로 사용하지 않았습니다.

답변

8

간단한 예 :

views.py 당신은 정확한 날짜와 달보기를 호출 할 필요가

from django.views.generic.dates import MonthArchiveView 
from myapp.models import Article 

urlpatterns = patterns('', 
    url(r'^articles/monthly/$',MonthArchiveView.as_view(
     model=Article, 
     paginate_by=12, 
     date_field='publish_date', 
     template_name='archive_templates/monthly.html', 
    ),name="monthly"), 
) 

, 그렇지 않으면 당신은 예외를 얻을 수 있습니다. 기본 인수는 이고, monthb (소문자의 짧은 월 이름)입니다.

여기 articles/monthly/?year=2012&month=feb

예를 호출하면 사용할 수있는 샘플 archive_templates/monthly.html이다.

우수한 twitter bootstrap 프레임 워크의 CSS 클래스를 사용하고 있습니다. 추천!

이 조각은 사용할 수 개월간 진행됩니다

{% if is_paginated %} 
    <div class="pagination pull-right"> 
     <ul> 
      <li class="{% if page_obj.has_previous %}prev {% else %} prev disabled {% endif %}"> 
       <a href="{% if page_obj.has_previous %}?page={{ page_obj.previous_page_number }}&year={{ month|date:"Y" }}&month={{ month|date:"b" }}{% else %}#{% endif %}">&larr;</a></li> 
      <li class="disabled"><a href="#"><strong>{{ page_obj.number }} of {{ paginator.num_pages }}</strong></a></li> 

      <li class="{% if page_obj.has_next %}next{% else %} next disabled {% endif %}"> 
       <a href="{% if page_obj.has_next %}?page={{ page_obj.next_page_number }}&year={{ month|date:"Y" }}&month={{ month|date:"b" }}{% else %}#{% endif %}">&rarr;</a> 
      </li> 

     </ul> 
    </div> 
{% endif %} 

객체의 실제 목록을 반복하는 것은 매우 간단하다 :

<table class="zebra-striped" width="100%"> 
    <thead> 
    <tr> 
     <th>#</th> 
     <th>Title</th> 
     <th>Author</th> 
     <th>Published On</th> 
    </tr> 
    </thead> 
    <tbody> 
    {% for obj in object_list %} 
     <tr> 
      <th>{{ forloop.counter }}</th> 
      <td>{{ obj.title }}</td> 
      <td>{{ obj.author }}</td> 
      <td>{{ obj.publish_date|date:"d/m/Y" }}</td> 
     </tr> 
    {% endfor %} 
    </tbody> 
</table> 

Archive for {{ month|date:"F" }} {{ month.year }}<br /> 
    <div class="pagination pull-left"> 
     <ul> 
      {% if previous_month %} 
       <li class="prev"> 
        <a href="{% url monthly %}?year={{ previous_month|date:"Y" }}&month={{ previous_month|date:"b" }}"> 
         &larr; {{ previous_month|date:"M Y" }} 
        </a> 
       </li> 
      {% endif %} 
      {% if next_month %} 
       <li class="next"> 
        <a href="{% url monthly %}?year={{ next_month|date:"Y" }}&month={{ next_month|date:"b" }}"> 
         {{ next_month|date:"M Y" }} &rarr;</a> 
       </li> 
      {% endif %} 
     </ul> 
    </div> 
{% endif %} 

이 조각은 페이지 매김을한다

여기에서 당신은 dev에 어떻게 알아 낼 수 있어야합니다 보관 메뉴를 빠져 나오십시오.

관련 문제