简体   繁体   中英

Customize change_list.html in django admin

change_list.html has Action part where a user selects an action and applies it on the selected items(queryset).

What I am trying to do is the following:

1. Add an additional <select> box near Action select box
2. Add an additional action which will use the value of the added select box in step 1.

I tried to customize change_list.html but adding an additional select box seemed quite difficult.

Is it possible? How can I do this?

If I understand you correctly, you want to make a custom Admin Action?

If so, start with the Django documentation for it . Then Have a look at these two use cases:

it is simple:

class YourModelAdmin(admin.ModelAdmin):
    class Media:
        js = ('/static/js/adminfix.js', )

    def get_urls(self):
        urls = super(YourModelAdmin, self).get_urls()
        my_urls = patterns('',
            (r'^custom_action_select/$', self.custom_func)
        )
        return my_urls + urls

    def custom_func(self, request):
        # your action

and your adminfix.js would look like:

(function($) {
   $(document).ready(function($) {
      $(".object-tools").append('<select id="actionid">stuff</select>');
      $(".object-tools").on('click', '#actionid', function(e){
          // you send here the request to /custom_action_select/
          // and handle if in custom_func() in your admin.py 
      });
   });
})(django.jQuery);

hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM