2012-02-06 7 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"), 
) 

が、そうでなければ、例外を取得します。デフォルト引数はY形式でyear、およびb(小文字の短い月名)でmonthです。

ここarticles/monthly/?year=2012&month=feb

例の呼び出しを使用すると、使用できるサンプルarchive_templates/monthly.htmlです。

私は優れたtwitter bootstrap枠組みからCSSクラスを使用しています。強くお勧めします!

このコードは、使用可能な数ヶ月を通過:

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

このコードは、ページネーションを行います。

{% 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> 
あなたはdevの方法を見つけ出すことができるはずですここから

あなたのアーカイブメニューをelopしてください。

関連する問題