Skip to content

Instantly share code, notes, and snippets.

@cassus
Last active September 28, 2022 18:15
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cassus/9757084 to your computer and use it in GitHub Desktop.
Save cassus/9757084 to your computer and use it in GitHub Desktop.
Django admin action as row button
class MyAdmin(admin.ModelAdmin):
list_display = (..., 'actions_html')
def actions_html(self, obj):
return format_html('<button class="btn" type="button" onclick="activate_and_send_email({pk})">Activate and send email</button>', pk=obj.pk)
actions_html.allow_tags = True
actions_html.short_description = "Actions"
activate_and_send_email = function(pk) {
$('input[name="_selected_action"]').removeAttr('checked');
$('input[name="_selected_action"][value="{}"]'.replace('{}', pk)).attr('checked', 'checked');
$('select[name="action"]').val('activate_account_and_send_email');
$('button[type="submit"][name="index"]').click();
};
@natoinet
Copy link

natoinet commented Oct 5, 2018

Great ! One question : Is there a way to clear the corresponding row checkbox after finishing the action ? Thank you !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment