Braham Shakti Braham Shakti - 1 year ago 239
Python Question

How to add button next to Add User button in Django Admin Site

I am working on Django Project where I need to extract the list of user to excel from the Django Admin's Users Screen. I added

variable to my Sample Class for getting the CheckBox before each user's id.

class SampleClass(admin.ModelAdmin):
actions =[make_published]

Action make_published is already defined. Now I want to append another button next to
Add user
button as shown in fig. Admin User Screen. But I dont know how can I achieve this this with out using new template. I want to use that button for printing selected user data to excel. Thanks, please guide me.

Answer Source
  1. Create a template in you template folder: admin/YOUR_APP/YOUR_MODEL/change_list.html
  2. Put this into that template

    {% extends "admin/change_list.html" %}
    {% block object-tools-items %}
        {{ block.super }}
            <a href="export/" class="grp-state-focus addlink">Export</a>
    {% endblock %}
  3. Create a view function in YOUR_APP/ and secure it with annotation

    from django.contrib.admin.views.decorators import staff_member_required
    def export(self, request):
        ... do your stuff ...
        return HttpResponseRedirect(request.META["HTTP_REFERER"])
  4. Add new url into YOUR_APP/ to url config for admin model

    from django.conf.urls import patterns, include, url
    class YOUR_MODELAdmin(admin.ModelAdmin):
        ... list def stuff ...
        def get_urls(self):
            urls = super(MenuOrderAdmin, self).get_urls()
            my_urls = patterns("",
                url(r"^export/$", export)
            return my_urls + urls

Enjoy ;)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download