Skip to main content

Django mixin to easily add buttons to any ModelAdmin

Project description

django-admin-extra-buttons

Pypi coverage Test Docs

my image

This is a full rewriting of the original django-admin-extra-url. It provides decorators to easily add custom buttons to Django Admin pages and/or add views to any ModelAdmin

It allows easy creation of wizards, actions and/or links to external resources as well as api only views.

Four decorators are available:

  • button() to mark a method as extra view and show related button
  • link() This is used for "external" link, where you don't need to invoke local views.
  • view() View only decorator, this adds a new url but do not render any button.
  • choice() Menu like button, can be used to group multiple @views().

How to use it

from admin_extra_buttons.api import ExtraButtonsMixin, button, confirm_action, link, view
from admin_extra_buttons.utils import HttpResponseRedirectToReferrer
from django.http import HttpResponse, JsonResponse
from django.contrib import admin
from django.views.decorators.clickjacking import xframe_options_sameorigin
from django.views.decorators.csrf import csrf_exempt


class MyModelModelAdmin(ExtraButtonsMixin, admin.ModelAdmin):

    @button(permission='demo.add_demomodel1',
            visible=lambda self: self.context["request"].user.is_superuser,
            change_form=True,
            html_attrs={'style': 'background-color:#88FF88;color:black'})
    def refresh(self, request):
        self.message_user(request, 'refresh called')
        # Optional: returns HttpResponse
        return HttpResponseRedirectToReferrer(request)

    @button(html_attrs={'style': 'background-color:#DC6C6C;color:black'})
    def confirm(self, request):
        def _action(request):
            pass

        return confirm_action(self, request, _action, "Confirm action",
                              "Successfully executed", )

    @link(href=None,
          change_list=False,
          html_attrs={'target': '_new', 'style': 'background-color:var(--button-bg)'})
    def search_on_google(self, button):
        original = button.context['original']
        button.label = f"Search '{original.name}' on Google"
        button.href = f"https://www.google.com/?q={original.name}"

    @view()
    def select2_autocomplete(self, request):
        return JsonResponse({})

    @view(http_basic_auth=True)
    def api4(self, request):
        return HttpResponse("Basic Authentication allowed")

    @view(decorators=[csrf_exempt, xframe_options_sameorigin])
    def preview(self, request):
        if request.method == "POST":
            return HttpResponse("POST")
        return HttpResponse("GET")

Project Links

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_admin_extra_buttons-1.6.0.tar.gz (188.1 kB view details)

Uploaded Source

Built Distribution

django_admin_extra_buttons-1.6.0-py2.py3-none-any.whl (21.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django_admin_extra_buttons-1.6.0.tar.gz.

File metadata

File hashes

Hashes for django_admin_extra_buttons-1.6.0.tar.gz
Algorithm Hash digest
SHA256 bf260aa654f82e125718aa337f8acdaef46959dc981cf46cd5cde2db577c0bd5
MD5 1cf29c3b3ceed37e6df1f0f97196bebd
BLAKE2b-256 1ead100d68b4a7410d3cb2c0b56032c19816e73790b3db70639906b6aac6ffcb

See more details on using hashes here.

File details

Details for the file django_admin_extra_buttons-1.6.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_admin_extra_buttons-1.6.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 fb79cca443866679a97d769d781ff4ff921ea68cdb28873d7e00a70a9c90e24f
MD5 8d4bd58037fddec833385fa22b850039
BLAKE2b-256 e20ccd02498331a6bf9ff4472765f650296afa058c24da596ac340a639dba7be

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page