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 required argument is queryset.
The optional arguments are paginate_by, page, template_name, template_loader, extra_context, allow_empty, context_processors, template_object_name, and mimetype.
The context variables are object_list, is_paginated, results_per_page, has_next, has_previous, page, next, previous, last_on_page, first_on_page, pages, and hits.
The default template_name is app_label/model_name_list.html (such as People/Person_list.html).
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 ObjectsIn 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:
|