Categorized | Django, Featured


Adding Views to the Django Admin

Posted on 26 March 2010 by BeardyGeek

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(admin.site.urls)),

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
...
@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',
  context_instance=RequestContext(request))

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",
         widget=widgets.AdminDateWidget())

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>
{{ form.media }}

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" />

Breadcrumbs

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>
      </li>
    </ul>
</div>

Enjoy!

Bookmark and Share

Related Posts

  • Mike
    Great Post!

    This really helped. Thanks!
  • sorl
    get_urls is in ModelAdmin, not AdminSite, it is the preferred way to do this and it is not harder:

    def get_urls(self):
    mypatterns + super(MyAdmin, self).get_urls()
  • stuartk
    get_urls is in both AdminSite and ModelAdmin (see http://docs.djangoproject.com/en/dev/ref/contri...). And I guess it's not really harder, but it is more code to override get_urls than to add a single line to url patterns.
blog comments powered by Disqus

Twitter Buttons