Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 actions 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.

like image 818
Braham Shakti Avatar asked Jul 31 '13 11:07

Braham Shakti


People also ask

How do I add a button to a Django project?

Django >1.8 Another option for adding a button would be to use django-object-actions. First, install it: pip install django-object-actions . (Also add django-object-actions to your requirements file if you have one). Second, add django_object_actions to your INSTALLED_APPS .

Can I use Django admin as frontend?

Django Admin is one of the most important tools of Django. It's a full-fledged application with all the utilities a developer need. Django Admin's task is to provide an interface to the admin of the web project. Django's Docs clearly state that Django Admin is not made for frontend work.


1 Answers

  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 }}      <li>         <a href="export/" class="grp-state-focus addlink">Export</a>     </li>  {% endblock %} 
  3. Create a view function in YOUR_APP/admin.py and secure it with annotation

    from django.contrib.admin.views.decorators import staff_member_required  @staff_member_required def export(self, request):      ... do your stuff ...      return HttpResponseRedirect(request.META["HTTP_REFERER"]) 
  4. Add new url into YOUR_APP/admin.py 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 ;)

like image 135
n1_ Avatar answered Sep 26 '22 06:09

n1_