[Django]-How to use date based views in Django

8👍

The simplest example:

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

You need to call the view with a correct day and month, otherwise you’ll get an exception. Default arguments are year in Y format, and month in b (lowercase short month name).

Example call articles/monthly/?year=2012&month=feb

Here is a sample archive_templates/monthly.html you can use.

I am using css classes from the excellent twitter bootstrap framework. Highly recommended!

This snippet goes through the available months:

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

This snippet does the pagination:

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

The actual list of objects is very simple to iterate over:

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

From here you should be able to figure out how to develop your archive menu.

Leave a comment