如何在Django中使用基于日期的视图

Sac*_*hin 3 django django-generic-views

这可能是一个小小的天真问题,但我一直试图理解如何在django中使用新的 基于日期的视图,但没有一个例子,我处于死胡同.我想要做的是在页面上显示我的所有博客条目(带分页),在侧面导航中我想显示根据年份和月份完成的归档.

我想要的是非常基本的,可以在下面的图片中看到.

在此输入图像描述

如果有人可以给我一个例子那么它会非常棒.我可以处理模板,但只需要知道如何使用基于类的通用视图.如果是通用视图,我真的没用过多.

Bur*_*lid 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"),
)
Run Code Online (Sandbox Code Playgroud)

您需要使用正确的日期和月份调用视图,否则您将获得异常.默认参数yearY格式,并monthb(小写的短月份名称).

示例电话 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 %}
Run Code Online (Sandbox Code Playgroud)

这个片段做了分页:

{% 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 %}
Run Code Online (Sandbox Code Playgroud)

迭代的实际对象列表非常简单:

<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>
Run Code Online (Sandbox Code Playgroud)

从这里,您应该能够弄清楚如何开发存档菜单.