Beardy Geek – Web Development Blog

A blog about web development – giving tips and tutorials about all aspects of web dev.

Beardy Geek – Web Development Blog

Adding Views to the Django Admin

I recently had to write a view that exported data to csv.  The view had to be contained within the Django admin area, on a separate page from the rest of the apps and models.  Here is the approach I took.

The Urls

There are 2 ways to add urls to the admin: the quick and easy way, or the not so quick and easy way.  In this case, as I was only adding a single url, I went for the quick and easy way.  The other way involves extending the get_urls() method of AdminSite, adding to the existing admin url pattern.  I did this:

(r'^admin/mypage/$', 'myapp.views.my_admin_view'),
(r'^admin/', include(,

Make sure you add your page before the main admin include.

The View

When you write your view make sure you use the staff_member_required decorator.

from django.contrib.admin.views.decorators import staff_member_required
def my_admin_view(request):
    # view code

When rendering your view, also make sure you pass RequestContext to the template:

return render_to_response('my_template.html',

The Template

So that your view looks like it belongs in the admin area, you need to extend the admin base site template:

{% extends 'admin/base_site.html' %}

You’ll also want to add in i18n support, and the admin media:

{% load i18n adminmedia %}

If you want to add in extra styles you can use {% block extrastyle %}, and you could put javascript in {% block extrahead %}.

Adding form date widgets

If you have a form on your page that includes date fields, you might want to have the nice django javascript form widgets. First you need to add the widgets to your form:

from django.contrib.admin import widgets

class AdminForm(forms.Form):
    start_date = forms.DateField(label="Start date",

Next you need to add in the following to your extrahead block (replace admin-media with whatever you’ve set your admin media url to):

<script type="text/javascript" src="/admin/jsi18n/"></script>
<script type="text/javascript" src="/admin-media/js/core.js"></script>
{{ }}

You also need to add the admin forms css in the extrastyles block:

{{ block.super }}
<link rel="stylesheet" type="text/css" href="/admin-media/css/forms.css" />


If you want to have the breadcrumb trail at the top of the page, you need to create your own breadcrumb:

{% block breadcrumbs %}<div class="breadcrumbs"><a href="/admin/">
{% trans "Home" %}</a> > My View</div>{% endblock %}

Adding a link to the admin index

When I add a link in, I like it to go in one of the right column boxes, so I usually create a new one and put it above the recent actions box. To do this, you need to override the admin index.html file.

Create a directory in your templates directory called admin. Then copy the index.html file from django/contrib/admin/templates to your new directory.

You’ll see a block called ‘sidebar’. Under the ‘content-related’ div, place the following:

<div class="module">
  <h2>Admin tools</h2>
    <ul class="actionlist">
      <li class="changelink">
        <a href="/admin/mypage/">My View</a>