" />
Menu
×
×
   ❮   
PYTHON FOR DJANGO DJANGO FOR BEGINNERS DJANGO SPECIFICS PAYMENT INTEGRATION API BASICS Roadmap
     ❯   

ADMIN PANEL CUSTOMIZATION

Custom buttons and actions

×

Share this Topic

Share Via:

Thank you for sharing!


Adding Custom Options to the Admin Action Dropdown

The Django admin panel provides a default "Actions" dropdown that allows you to apply operations to selected objects. You can extend this functionality by adding custom actions. Follow these steps to create and register custom admin actions:

1. Define a Custom Action

Create a function that defines the logic for your custom action. This function receives three arguments:

  • modeladmin: The current model admin instance.
  • request: The HTTP request object.
  • queryset: The queryset of selected objects.

Example:

def mark_as_published(modeladmin, request, queryset):
    updated_count = queryset.update(is_published=True)
    modeladmin.message_user(
        request,
        f"{updated_count} book(s) marked as published."
    )

mark_as_published.short_description = "Mark selected books as Published"

2. Register the Action

Include the custom action in the actions attribute of your model admin class:

Example:

@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'is_published')
    actions = [mark_as_published]

3. Result

The action will now appear in the "Actions" dropdown in the Django admin panel for the Book model. Admin users can select multiple records and apply the action.

Adding Custom Buttons to the Admin Panel

In addition to actions, you can add custom buttons to the Django admin interface. These buttons can trigger specific views or execute logic directly.

1. Create a Custom Admin View

Define a method in your model admin class to handle the button click:

Example:

from django.urls import path
from django.http import HttpResponse

@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
    list_display = ('title', 'is_published')

    def get_urls(self):
        urls = super().get_urls()
        custom_urls = [
            path('custom-action/', self.custom_action_view, name='custom-action')
        ]
        return custom_urls + urls

    def custom_action_view(self, request):
        # Custom logic goes here
        self.message_user(request, "Custom action executed successfully!")
        return HttpResponse("Custom action executed!")

2. Add a Button to the Admin Template

Override the change_list.html template to include a custom button:

Example:

{% extends "admin/change_list.html" %}

{% block object-tools-items %}
    {{ block.super }}
    
        <a class="button" href="{% url 'admin:custom-action' %}">Custom Action </a>
    
{% endblock %}

References


Django-tutorial.dev is dedicated to providing beginner-friendly tutorials on Django development. Examples are simplified to enhance readability and ease of learning. Tutorials, references, and examples are continuously reviewed to ensure accuracy, but we cannot guarantee complete correctness of all content. By using Django-tutorial.dev, you agree to have read and accepted our terms of use , cookie policy and privacy policy.

© 2025 Django-tutorial.dev .All Rights Reserved.
Django-tutorial.dev is styled using Bootstrap 5.
And W3.CSS.