Previous Page Next Page

Displaying Object Details in a Generic View

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

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

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

2.
Add the following code that accesses the blog context variable, which will be defined later in the URLconf file, to display the details of the Quote object:

<font size="5" color="white">{{ blog.title }}</font>
<font size="2">{{ blog.date }}</font>

3.
Add the rest of the code, shown in Listing 12.3, 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_details.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 Blog model:

from django.views.generic import list_detail
from iFriends.People.models import Blog

7.
Add the following lines of code, shown in Listing 12.4, to create a dictionary to use for the object_details view that sets the object name to blog and defines a custom template file:

blog_detail_info = {
    'queryset' : Blog.objects.all(),
    'template_object_name' : 'blog',
    'template_name' : 'Blogs/blog_detail.html',
}

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

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

9.
Save the iFriends/urls.py file.

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

http://127.0.0.1:8000/generic/blog_details/1/



Figure 12.2. Generic object details view for a Blog object.


Listing 12.3. Full Contents of iFriends/templates/Blogs/blog_detail.html

{% extends "iFriends_base.html" %}

{% block title %}Generic Blog Details{% endblock %}
{% block content %}
<table width="400">
<tr bgcolor="aabbcc"><td colspan="3">
<font size="5" color="white">{{ blog.title }}</font>
<font size="2">{{ blog.date }}</font>
</td></tr>
<tr valign="top"><td bgcolor="ddffff">
{{ blog.text }}
</td></tr>
{% endblock %}

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

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

blog_detail_info = {
    'queryset' : Blog.objects.all(),
    'template_object_name' : 'blog',
    'template_name' : 'Blogs/blog_detail.html',
}

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


Previous Page Next Page