Add admin edit form buttons (work with django 1.6)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#
# Copy from django source template admin/change_form.html to your app template dir
# And replace the block 
# "./your_app/templates/admin/change_form.html"

{% block object-tools %}
  {% if change %}{% if not is_popup %}
  <ul class="object-tools">
  {% for button in buttons %}
     <li><a {% if button.confirm %} onclick="return confirm('{{ button.confirm }}')" {% endif %} 
        href="./{{ button.url }}/">{{ button.textname }}</a></li>
  {% endfor %}
  <li><a href="history/" class="historylink">History</a></li>
  {% if has_absolute_url %}<li><a href="../../../r/{{ content_type_id }}/{{ object_id }}/" class="viewsitelink">View on site</a></li>{% endif%}
  </ul>
  {% endif %}{% endif %}
{% endblock %}


#
# Add this code to your models.py
#

def _ban_user(request, id):
    from django.http.response import HttpResponse
    return HttpResponse(u'User %s Banned! :)' % id)

class MyUserAdmin(UserAdmin):
    buttons = [
        {
         'url': '_ban_action',
         'textname': 'Ban user',
         'func': _ban_user,
         'confirm': u'Do you want ban this user?'
        },
    ]

    def change_view(self, request, object_id, form_url='', extra_context={}):
        extra_context['buttons'] = self.buttons
        return super(MyUserAdmin, self).change_view(request, object_id, form_url, extra_context=extra_context)

    def get_urls(self):
        from django.conf.urls import patterns, url, include
        urls = super(MyUserAdmin, self).get_urls()
        my_urls = list( (url(r'^(.+)/%(url)s/$' % b, self.admin_site.admin_view(b['func'])) for b in self.buttons) )
        return my_urls + urls
        

# Enjoy!

More like this

  1. Add a button to the top of the admin change form by tsaylor 4 years, 1 month ago
  2. Add buttons in admin forms by mamat 5 years, 7 months ago
  3. newforms-admin edit callback-url hook by myers 6 years ago
  4. Multiple-Submit-Button Widget for Choice Field by Archatas 5 years, 8 months ago
  5. Admin Save and view next button by ungenio41 3 years, 11 months ago

Comments

morlandi (on March 24, 2014):

thank you

#

morlandi (on March 25, 2014):

Might be extended to add buttons to changelist view:

class MyUserAdmin(UserAdmin):

    change_list_buttons = [{
            'url': 'download',
            'textname': _(u'Download'),
            #'func': _ban_user,
            'confirm': _(u'Download all remote objects ?')
        },
    ]

    def changelist_view(self, request, extra_context={}):
        extra_context['change_list_buttons'] = self.change_list_buttons
        return super(SlowdShopBaseModelAdmin, self).changelist_view(request, extra_context=extra_context)

Then in change_list.html:

{% block object-tools-items %}
    {% for button in change_list_buttons %}
        <li>
            <a {% if button.confirm %} onclick="return confirm('{{ button.confirm }}')" {% endif %} href="./{{ button.url }}/">{{ button.textname }}</a>
        </li>
    {% endfor %}
    {{ block.super }}
{% endblock object-tools-items %}

#

(Forgotten your password?)