3

少し素朴な質問かもしれませんが、 djangoで新しい日付ベースのビューを使用する方法を理解しようとしています が、例がなければ行き詰まっています。私がやりたいことは、すべてのブログエントリをページ (ページネーション付き) に表示し、サイドナビゲーションで、年と月に応じてアーカイブを表示したいことです。

私が欲しいのは非常に基本的なもので、下に添付された写真で見ることができます.

ここに画像の説明を入力

誰かが私に例を提供できれば、それは本当に素晴らしいことです. テンプレートは処理できますが、クラス ベースのジェネリック ビューの使用方法を知る必要があるだけです。一般的なビューの場合、私は実際にはあまり使用していません。

4

1 に答える 1

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

正しい日と月でビューを呼び出す必要があります。そうしないと、例外が発生します。デフォルトの引数はyearin Yformat およびmonthin b(小文字の短い月名) です。

呼び出しの例articles/monthly/?year=2012&month=feb

archive_templates/monthly.html使用できるサンプルを次に示します。

優れたTwitter ブートストラップフレームワークの 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>

ここから、アーカイブ メニューを開発する方法を理解できるはずです。

于 2012-02-06T05:41:25.097 に答える