The django.views.generic.list_detail.object_detail view creates a generic view for rendering a specific object. The object_detail view requires a queryset argument that contains objects to search, and an object_id argument that contains the primary key for the object you want to display.
The following list describes the required arguments, optional arguments, and context variables available when using the object_details 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_details.html (such as People/Person_details.html).
The following URLconf code snippet shows an example of defining a generic object_details view that retrieves the object_id variable from the URL and assigns the object name to blog in the context variables:
detail_info = { 'queryset' : Blog.objects.all(), 'template_object_name' : 'blog', } urlpatterns = patterns('django.views.generic.list_detail', (r'^blog_detail/(?P<object_id>\d+)/$', object_details, detail_info), }
The following template code snippet shows an example of accessing the blog context variable just assigned in the template:
<h1>{{ blog.title }}</li> {{ blog.text }}
Try It Yourself: Use a Generic View to Display Object DetailsIn this section, you will use the object_details generic view to display the fields of a Blog object. Follow these steps to create a template and update the URLconf file to enable the view:
|