views:

72

answers:

3

Hi, I have an application in Django with a routine which would be available only to the admin. I'm quite new to the python/django world, so maybe my question is trivial. What I want to do is add a button to perform the routine in this application's section of the admin app.

I'm quite confused from there, am I suppose to make a template for it, and if it's the case, how do I add a html template for an app in the admin. Or maybe there's a command to simply add a button?

Thanks for your time

A: 

Don't mess with the admin pages.

  1. Create an "application" for this. Yes, your function is just a "routine". That's okay. Many smaller applications are a good thing.

  2. This application has nothing new in models.py. No new model. Zero lines of code.

  3. This application has a useful URL in urls.py. Something that can be used to display this admin page. One URL. Not many lines of code (less than a dozen.)

  4. This application has one view function in views.py. On "GET", this view function presents the form. On "POST", this view function does the "routine". This is the "heart" of your application. The GET -- of course -- simply returns the template for display. The POST does the real work, and returns a final status or something.

This view function is protected with a decorator so that only an admin can execute it. See http://docs.djangoproject.com/en/1.2/topics/auth/#django.contrib.auth.decorators.user_passes_test. You want to write a test for being an admin. lambda u: u.is_staff is probably it.

  1. This application has one template, presented by the GET and POST. That template has your form with your button. The one you can't add to admin easily.

  2. The tests.py is a test case with two users, one who is an admin and one who is not an admin.

No messing with built-in admin pages.

S.Lott
+2  A: 

If you have a routine for a part of admin, i think this is the best for you:

http://docs.djangoproject.com/en/1.2/ref/contrib/admin/actions/

diegueus9
A: 

Messing with the admin forms can be complicated but i've commonly found that adding links, buttons, or extra info is easy and helpful. (Like a list of links to related objects witout making an inline, esp for things that are more viewed than edited).

From Django docs

> Because of the modular design of the admin templates, it is usually
> neither necessary nor advisable to
> replace an entire template. It is
> almost always better to override only
> the section of the template which you
> need to change.

This will add a list over the top of the form.

Place in templates/admin/[your_app]/[template_to_override]

{% extends "admin/change_form.html" %}

{% block form_top %}

{% for item in original.items %}
  {{ item }}
{% endfor %}

{% endblock %}
Lincoln B