Previous Page Next Page

Creating Custom Admin Views

Another way to customize the admin interface for your website is to create your own custom views to be displayed in the admin interface. You can create your own custom view function and template file and then specify an admin path in the URLconf file when enabling it.

Creating custom views for the admin interface involves the same steps as creating a normal view. The view function can live in any application, and the template file can reside anywhere in the template path. The only real differences are that the URL to access the view begins with admin, and the view function should make certain that the user has staff privileges before allowing him or her to access the view.

Watch Out!

The admin URL patterns match just about every URL path you might try to use. So you need to add URL patterns for your custom views before including the django.contrib.admin.urls URLconf file in your own URLconf file.


You can use the staff_member_required decorator function to verify whether the user has staff privileges before giving him or her access to the view:

from django.contrib.admin.views.decorators import staff_member_required
@staff_member_required
def custom_admin_view(request):
. . .

Try It Yourself: Create a Custom Admin View

In this section, you will create a custom admin view function and template that render usage information about each user's blogs. You will define an URL pattern so that the view can be accessed as an admin URL. You will also add the staff_member_required decorator function to ensure that only users with admin privileges can access the view.

Follow these steps to create and enable the custom admin view:

1.
Create a directory called iFriends/templates/Custom in which to store the custom admin template.

2.
Create and open a template file called iFriends/templates/Custom/blog_usage.html in an editor.

3.
Add the following lines of code, shown in Listing 18.2, to extend the admin/base_site.html template and define the title block that the base_site.html template will render:

{% extends "admin/base_site.html" %}
{% block title %}Blog Usage Report{% endblock %}

4.
Add the full contents of the content block, shown in Listing 18.2, to generate the usage report.

5.
Save the iFriends/templates/Custom/blog_usage.html file.

6.
Open the iFriends/Custom/views.py file in an editor.

7.
Add the following import statements, shown in Listing 18.3. They will give you access to the render_to_response, Person, RequestContext, and staff_member_required functions and objects in the custom view function:

from django.shortcuts import render_to_response
from iFriends.People.models import Person
from django.template import RequestContext
from django.contrib.admin.views.decorators import staff_member_required

8.
Add the following code that defines the blog_usage() view function, shown in Listing 18.3. This generates a list containing the blog usage for each Person object in the database:

def blog_usage(request):
    pList = Person.objects.all()
    uList = []
    for p in pList:
        size = 0
        for b in p.blogs.all():
            size += b.blog_size()
        uList.append({'person': p,
                      'count': len(p.blogs.all()),
                      'size': size})

9.
Add the following render_to_response() return statement to the blog_usage() view function, shown in Listing 18.3. It renders the custom template created in steps 1 through 5:

return render_to_response('Custom/blog_usage.html',
                {'uList': uList},
                context_instance = RequestContext(request))

By the Way

You need to add RequestContext to the render_to_response() call so that the admin templates will have access to the User object in the request.

10.
Add the following decorator function the blog_usage() view function, listed in Listing 18.3, to ensure that the user is logged in and has staff privileges:

@staff_member_required

11.
Save the iFriends/Custom/views.py file.

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

13.
Add the following URL pattern, before you include django.contrib.admin.urls, to enable the blog_usage() function as an admin URL:

(r'^admin/blog_usage/$', 'iFriends.Custom.views.blog_usage'),

14.
Save the iFriends/urls.py file.

15.
Access the following URL in a browser to verify that the blog_usage() view function works, as shown in Figure 18.2:

http://127.0.0.1:8000/admin/blog_usage/

Figure 18.2. The custom blog_usage() function in the admin interface.


Listing 18.2. Full Contents of the iFriends/templates/Custom/blog_usage.html File

{% extends "admin/base_site.html" %}
{% block title %}Blog Usage Report{% endblock %}

{% block content %}
<h1>Blog Usage Report</h1>
<table>
<tr>
  <td>Person</td>
  <td>Blog Count</td>
  <td>Total Bytes</td>
</tr>
{% for item in uList %}
  <tr align="center">
    <td>{{ item.person.name }}</td>
    <td>{{ item.count }}</td>
    <td>{{ item.size }}</td>
  </tr>
{% endfor %}
</table>
{% endblock %}

Listing 18.3. Full Contents of the iFriends/Custom/views.py File

from django.shortcuts import render_to_response
from iFriends.People.models import Person
from django.template import RequestContext
from django.contrib.admin.views.decorators import staff_member_required

@staff_member_required
def blog_usage(request):
    pList = Person.objects.all()
    uList = []
    for p in pList:
        size = 0
        for b in p.blogs.all():
            size += b.blog_size()
        uList.append({'person': p,
                      'count': len(p.blogs.all()),
                      'size': size})

    return render_to_response('Custom/blog_usage.html',
                     {'uList': uList},
                     context_instance = RequestContext(request))


Previous Page Next Page