Previous Page Next Page

Displaying an Object List in a Generic View

The django.views.generic.list_detail.object_list view creates a generic view for rendering a list of objects. The object_list view requires a QuerySet argument, queryset, that contains the objects you want to list.

For example, if you wanted to list all the objects in the Blog model with a title that contains the word "test," you would make the following assignment to the queryset argument:

list_info = { 'queryset' : Blog.objects.filter(title__contains='test')}

The following list describes the required arguments, optional arguments, and context variables available when using the object_list view (see Tables 12.1 and 12.2 for information about the arguments and context variables):

The following code snippet shows an example of defining a generic object_list view in the URLconf file:

list_info = {
    'queryset' : Blog.objects.filter(title__contains='test'),
    'allow_empty' : True,
}
urlpatterns = patterns('django.views.generic.list_detail',
    (r'^blog_list/$', object_list, list_info),
}

The following template code snippet shows an example of accessing the context variable object_list in a template:

{% for blog in object_list %}
    <li>{{ blog.title }}</li>
{% endfor %}

Try It Yourself: Use a Generic View to Display a List of Objects

In this section, you will use the object_list generic view to display a list of Quote objects. Follow these steps to create a template and update the URLconf file to enable the view:

1.
Create and open a file called iFriends/templates/Quotes/quote_list.html in an editor.

2.
Add the following for loop code that accesses the object_list context variable to generate a list of Quote objects:

{% for q in object_list %}
    <li>
        {{ q.text }}
        <font size="3">-{{ q.by }}
        <a href="/generic/quote_update/{{q.id}}/">[Update]</a>
        <a href="/generic/quote_delete/{{q.id}}/">[Delete]</a>
        </font>
    </li>
{% endfor %}

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

4.
Save the iFriends/templates/Quotes/quote_list.html file.

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

6.
Add the following lines of code to import the list_detail view and the Quote model:

from django.views.generic import list_detail
from iFriends.Quotes.models import Quote

7.
Add the following lines of code, shown in Listing 12.2, to create a dictionary to use for the object_list view:

quote_list_info = {
    'queryset' : Quote.objects.all(),
    'allow_empty' : True,
}

8.
Add the following URL patterns to enable the generic object_list view:

urlpatterns += patterns('',
    (r'^generic/quote_list/$',
      list_detail.object_list, quote_list_info),
)

9.
Save the iFriends/urls.py file.

10.
Access the following URL in a browser, as shown in Figure 12.1, to verify that the object_list view works:

http://127.0.0.1:8000/generic/quote_list/

Figure 12.1. Generic object list view for the Quote objects.




Listing 12.1. Full Contents of iFriends/templates/Quotes/quote_list.html

{% extends "iFriends_base.html" %}

{% block title %}Quote List{% endblock %}
{% block content %}
<table width=100%>
<tr bgcolor="aabbcc"><td colspan="3">
<font size="5" color="white">Quote List</font>
</td></tr>
<tr valign="top"><td width=30% bgcolor=" aadddd ">
<font size="5">
<a href="/generic/quote_add">[Add]</a>
{% for q in object_list %}
    <li>
        {{ q.text }}
        <font size="3">-{{ q.by }}
        <a href="/generic/quote_update/{{q.id}}/">[Update]</a>
        <a href="/generic/quote_delete/{{q.id}}/">[Delete]</a>
        </font>
    </li>
{% endfor %}
</font></td></tr>
{% endblock %}

By the Way

The [Add], [Update], and [Delete] links are for generic views that will be created later in this hour.

Listing 12.2. Generic Section Fragment for Quote Objects in the iFriends/urls.py File

. . .
#Quote generic section
from django.views.generic import list_detail
from iFriends.Quotes.models import Quote

quote_list_info = {
    'queryset' : Quote.objects.all(),
    'allow_empty' : True,
}

urlpatterns += patterns('',
    (r'^generic/quote_list/$',
      list_detail.object_list, quote_list_info),
)


Previous Page Next Page