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.

Three 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.

Install

pip install django-admin-extra-buttons

After installation add it to INSTALLED_APPS

INSTALLED_APPS = (
   ...
   'admin_extra_buttons',
)

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',
            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

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.4.2.tar.gz (178.8 kB view details)

Uploaded Source

Built Distribution

django_admin_extra_buttons-1.4.2-py2.py3-none-any.whl (19.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-admin-extra-buttons-1.4.2.tar.gz.

File metadata

  • Download URL: django-admin-extra-buttons-1.4.2.tar.gz
  • Upload date:
  • Size: 178.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.8

File hashes

Hashes for django-admin-extra-buttons-1.4.2.tar.gz
Algorithm Hash digest
SHA256 dfdea52416e84bb919050d4369767389a5cc0b2718bd0969e5361427f25ccfa6
MD5 861a2785fa2376ca448934f20dd18268
BLAKE2b-256 d23a6fb7b2624c2c9c5b248ba663c97858f765878063c8274bd75e882fc0fab2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: django_admin_extra_buttons-1.4.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 19.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.50.0 CPython/3.7.8

File hashes

Hashes for django_admin_extra_buttons-1.4.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 3715ecf1e2343dc88e930f1deb1b96029886c385f8ab1888a0e594fbf6abfe28
MD5 468246ccdda8a0a31d90a5b5f57f5bbb
BLAKE2b-256 e53ca30695ddaa0448bca0a62fda271eb1826d5b868985f3ab86f7c1d84d28fd

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