Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a custom button to the admin listing?

Tags:

django-admin

What I want is to put a custom button in each row of a page of the admin listing.
These buttons will have a function associate to it acting over that line.
I've already knew the "admin actions", but it's not what I want, ok?

Thank you!

like image 424
Jayme Tosi Neto Avatar asked May 12 '10 17:05

Jayme Tosi Neto


People also ask

How do you put a button on the list page for each row in Django admin page?

In the case of a button, you'll want to use format_html . This will mark the relevant parts of the string safe, and so that it's displayed as an HTML button, and escape the other parts to protect against various securit issues. The values of object.id and object. some_property will get inserted into the placeholders.


1 Answers

You can declare in your ModelAdmin a function to generate the html for your button, e.g.

    def button(self, obj):
        return mark_safe('<input type="...">')
    title.short_description = 'Action'
    title.allow_tags = True

And then put it in your in your list_display-tuple.

class MyAdmin(admin.ModelAdmin)
    list_display=('name', 'button')

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#modeladmin-options

like image 134
Bernhard Vallant Avatar answered Oct 26 '22 19:10

Bernhard Vallant