Login

Add admin edit form buttons (work with django 1.6)

Author:
shoreward
Posted:
December 10, 2013
Language:
Python
Version:
1.6
Score:
1 (after 1 ratings)

I can't find any workable solution, so I wrote it.

(Checked it on 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. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 8 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 %}

#

Please login first before commenting.