Previous Page Next Page

Displaying Date-Based Objects in a Generic View

Django provides several date-based generic views in the django.views.generic.date_based package to help you build views based on date Fields in the model. All these date-based views require a queryset argument that contains objects to search, and a date_field argument that contains the Field name to query when building the date-based view.

Each date-based view uses the date_field argument to query the queryset based on day, month, week, or year arguments to generate a list of either dates or objects.

The following sections describe each of the date-based views and list the required arguments, optional arguments, and context variables (see Tables 12.1 and 12.2 for information about the arguments and context variables).

Did you Know?

If the date in the object's date_field is in the future, it is not displayed by the date-based views unless the allow_future argument is set to True.


archive_index

The archive_index view is used to generate a year-based index view. Basically, the archive_index view creates a date object for each different year represented in the queryset and sends that list the template as the date_list variable. You can then use the date_list variable to list the years represented in the index.

archive_year

The archive_year view is used to generate a month-based index view. In addition to the queryset and date_field arguments, the archive_year view requires a year argument. The queryset is reduced to objects which have a date_field that matches the year argument.

The archive_year view creates a date object for each month represented in the queryset and sends that list the template as the date_list variable. You can then use the date_list variable to list the years represented in the index.

archive_month

The archive_month view is used to generate a month-based object view. In addition to the queryset and date_field arguments, the archive_month view requires year and month arguments. The queryset is reduced to objects whose date_field matches the year and month arguments.

The archive_month view sends that queryset to the template as the object_list variable. You can then use the object_list variable to list the objects in the template.

archive_week

The archive_week view is used to generate a week-based object view. In addition to the queryset and date_field arguments, the archive_week view requires year and week arguments. The queryset is reduced to objects whose date_field matches the year and week arguments.

The archive_week view sends that queryset to the template as the object_list variable. You can then use the object_list variable to list the objects in the template.

archive_day

The archive_day view is used to generate a day-based object view. In addition to the queryset and date_field arguments, the archive_month view requires year, month and day arguments. The queryset is reduced to objects whose date_field matches the year, month, and day arguments.

The archive_day view sends that queryset to the template as the object_list variable. You can then use the object_list variable to list the objects in the template.

archive_today

The archive_today view is the same as the archive_day view, except that it does not require the year, month, and day arguments. Instead, it uses the current system values of year, month, and day.

archive_detail

The archive_detail view works similar to the archive_day view, with two exceptions. The first is that it requires an additional object_id or slug argument. The second is that the only context variables available are extra_context and object. The object variable allows you to access the object to display the details.

URL Patterns for Date-Based Views

The following code illustrates the basic URL patterns for the archive_day, archive_week, archive_month, archive_year, and archive_index views, respectively:

urlpatterns = patterns('django.views.generic.date_based',
   (r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/(?P<slug>[-\w]+)/$',
      'object_detail', info_dict),
   (r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/$',
      'archive_day',   info_dict),
   (r'^(?P<year>\d{4})/(?P<week>\d{2})/$',
      'archive_week', info_dict),
   (r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$',
      'archive_month', info_dict),
   (r'^(?P<year>\d{4})/$', 'archive_year', info_dict),
   (r'^$','archive_index', info_dict),
)


					  

Try It Yourself: Use a Generic View to Display a Date-Based Index of Objects

In this section, you will use the archive_index, archive_year, and archive_month generic views to display a date-based index of Blog objects. Follow these steps to create a template for year index, month index, and blog index views and update the URLconf file to enable the views:

1.
Create and open a file called iFriends/templates/Blogs/blog_arch.html in an editor.

2.
Add the following for loop code to the file to generate a list of years containing Blog objects and add links to the blog_year index view for each year:

{% for date in date_list %}
    <li>
    <a href="/generic/blog_arch/{{ date|date:"Y" }}/">
        {{ date|date:"Y" }}</a>
    </li>
{% endfor %}

3.
Add the rest of the code, shown in Listing 12.5, to extend the site base template and add the rest of the supporting HTML code for the view.

4.
Save the iFriends/templates/Blogs/blog_arch.html file.

5.
Create and open a file called iFriends/templates/Blogs/blog_year.html in an editor.

6.
Add the following for loop code to the file to generate a list of months containing Blog objects and add links to the blog_month index view for each month:

{% for date in date_list %}
    <li>
    <a href="/generic/blog_arch/{{date|date:"Y"}}/{{date|date:"b"}}/">
        {{ date|date:"F" }}</a>
    </li>
{% endfor %}

7.
Add the rest of the code, shown in Listing 12.6, to extend the site base template and add the rest of the supporting HTML code for the view. The year context variable is accessed to display the year in the heading.

8.
Save the iFriends/templates/Blogs/blog_year.html file.

9.
Create and open a file called iFriends/templates/Blogs/blog_month.html in an editor.

10.
Add the following for loop code to the file to generate a list of Blog objects and add links to the generic blog_details view for each Blog:

{% for blog in object_list %}
    <li>
    <a href="/generic/blog_details/{{blog.id}}/">{{ blog }}</a>
    </li>
{% endfor %}

11.
Add the rest of the code, shown in Listing 12.7, to extend the site base template and add the rest of the supporting HTML code for the view. The month context variable is accessed to display the textual month in the heading.

12.
Save the iFriends/templates/Blogs/blog_month.html file.

13.
Open the iFriends/urls.py file in an editor.

14.
Modify the following lines of code to import the date_based view package:

from django.views.generic import list_detail, date_based

15.
Add the following lines of code, shown in Listing 12.8, to create a dictionary to use for the blog_arch view that sets the queryset date_field to blog and defines a custom template file:

blog_arch_info = {
    'queryset' : Blog.objects.all(),
    'date_field' : 'date',
    'template_name' : 'Blogs/blog_arch.html',
}

16.
Add the following lines of code, shown in Listing 12.8, to create a dictionary to use for the blog_year view that sets the queryset date_field to blog and defines a custom template file:

blog_month_info = {
    'queryset' : Blog.objects.all(),
    'date_field' : 'date',
    'template_name' : 'Blogs/blog_month.html',
}

17.
Add the following lines of code, shown in Listing 12.8, to create a dictionary to use for the blog_month view that sets the queryset date_field to blog and defines a custom template file:

blog_year_info = {
    'queryset' : Blog.objects.all(),
    'date_field' : 'date',
    'template_name' : 'Blogs/blog_year.html',
}

18.
Add the following URL patterns, shown in Listing 12.8, to enable the generic blog_arch, blog_year, and blog_month views:

urlpatterns += patterns('',
    (r'^generic/blog_arch/(?P<year>\d{4})/(?P<month>[a-z]{3})/$',
      date_based.archive_month, blog_month_info),
    (r'^generic/blog_arch/(?P<year>\d{4})/$',
      date_based.archive_year, blog_year_info),
    (r'^generic/blog_arch/$',
      date_based.archive_index, blog_arch_info),
)

19.
Save the iFriends/urls.py file.

20.
Access the following URL in a browser, as shown in Figure 12.3, to verify that the blog_arch view works:

http://127.0.0.1:8000/generic/blog_arch/



Figure 12.3. Generic date-based index view for Blog objects showing years that have Blog objects archived.


21.
Click one of the year links to access the blog_year view, shown in Figure 12.4, to verify that the blog_year view works.

Figure 12.4. Generic date-based year view for Blog objects showing months that have Blog objects archived.


22.
Click one of the month links to access the blog_month view, shown in Figure 12.5, to verify that the blog_month view works.

Figure 12.5. Generic date-based month view for Blog objects showing Blog objects archived for that month.


23.
Click one of the blog links to access the blog_details view, shown in Figure 12.2, to verify that the blog_year link works.

Listing 12.5. Full Contents of iFriends/templates/Blog/blog_arch.html

{% extends "iFriends_base.html" %}

{% block title %}Generic Blog List{% endblock %}
{% block content %}
<table width=100%>
<tr bgcolor="aabbcc"><td colspan="3">
<font size="5" color="white">Blog Archive</font>
</td></tr>
<tr valign="top"><td width=30% bgcolor="aaddee">
<font color="white" size="5">
{% for date in date_list %}
    <li>
    <a href="/generic/blog_arch/{{ date|date:"Y" }}/">
        {{ date|date:"Y" }}</a>
    </li>
{% endfor %}
</font></td></tr>
{% endblock %}



Listing 12.6. Full Contents of iFriends/templates/Blogs/blog_year.html

{% extends "iFriends_base.html" %}

{% block title %}Generic Blog List{% endblock %}
{% block content %}
<table width=100%>
<tr bgcolor="aabbcc"><td colspan="3">
<font size="5" color="white">Blog Archive {{ year }}</font>
</td></tr>
<tr valign="top"><td width=30% bgcolor="aaddee">
<font color="white" size="5">
{% for date in date_list %}
    <li>
    <a href="/generic/blog_arch/{{date|date:"Y"}}/{{date|date:"b"}}/">
        {{ date|date:"F" }}</a>
    </li>
{% endfor %}
</font></td></tr>
{% endblock %}

Listing 12.7. Full Contents of iFriends/templates/ Blogs/blog_month.html

{% extends "iFriends_base.html" %}

{% block title %}Generic Blog List{% endblock %}
{% block content %}
<table width=100%>
<tr bgcolor="aabbcc"><td colspan="3">
<font size="5" color="white">
Blog Archive {{ month|date:"F Y" }}
</font></td></tr>
<tr valign="top"><td width=30% bgcolor="aaddee">
<font color="white" size="5">
{% for blog in object_list %}
    <li>
    <a href="/generic/blog_details/{{blog.id}}/">{{ blog }}</a>
    </li>
{% endfor %}
</font></td></tr>
{% endblock %}

Listing 12.8. Generic Section for Blog Objects in the iFriends/urls.py File

#Blog generic section
from django.views.generic import list_detail, date_based
from iFriends.People.models import Blog

blog_detail_info = {
    'queryset' : Blog.objects.all(),
    'template_object_name' : 'blog',
    'template_name' : 'Blogs/blog_detail.html',
}
blog_arch_info = {
    'queryset' : Blog.objects.all(),
    'date_field' : 'date',
    'template_name' : 'Blogs/blog_arch.html',
    'allow_future': True
}
blog_month_info = {
    'queryset' : Blog.objects.all(),
    'date_field' : 'date',
    'template_name' : 'Blogs/blog_month.html',
}
blog_year_info = {
    'queryset' : Blog.objects.all(),
    'date_field' : 'date',
    'template_name' : 'Blogs/blog_year.html',
}

urlpatterns += patterns('',
    (r'^generic/blog_details/(?P<object_id>\d+)/$',
      list_detail.object_detail, blog_detail_info),
)

urlpatterns += patterns('',
    (r'^generic/blog_arch/(?P<year>\d{4})/(?P<month>[a-z]{3})/$',
      date_based.archive_month, blog_month_info),
    (r'^generic/blog_arch/(?P<year>\d{4})/$',
      date_based.archive_year, blog_year_info),
    (r'^generic/blog_arch/$',
      date_based.archive_index, blog_arch_info),
)


					  


Previous Page Next Page