A Django app for adding modal actions to the admin interface
Project description
Django Modal Actions
Django Modal Actions is a reusable Django app that provides a convenient way to add modal-based actions to your Django admin interface. It allows you to create custom actions that open in a modal dialog, enhancing the user experience and functionality of your Django admin.
Features
- Easy integration with Django admin
- Support for both list-view and object-view actions
- Customizable modal forms
- AJAX-based form submission
Requirements
- Python (>= 3.7)
- Django (>= 3.2)
Installation
-
Install the package using pip:
pip install django-modal-actions
-
Add
'django_modal_actions'
to yourINSTALLED_APPS
setting:INSTALLED_APPS = [ ... 'django_modal_actions', ... ]
Usage
-
In your
admin.py
, import the necessary components:from django.contrib import admin from django_modal_actions import ModalActionMixin, modal_action
-
Create a custom admin class that inherits from
ModalActionMixin
and your base admin class:@admin.register(YourModel) class YourModelAdmin(ModalActionMixin, admin.ModelAdmin): modal_actions = ["your_object_action"] list_modal_actions = ["your_list_action"] @modal_action(modal_header="Object Action") def your_object_action(self, request, obj, form_data=None): # Your object action logic here return "Action completed successfully" @modal_action(modal_header="List Action") def your_list_action(self, request, queryset, form_data=None): # Your list action logic here return f"Action completed on {queryset.count()} items"
-
If you need a custom form for your action, create a form class:
from django import forms class CustomForm(forms.Form): name = forms.CharField(label="Name", required=True) def clean_name(self): name = self.cleaned_data["name"] if name == "bad": raise forms.ValidationError("Name cannot be 'bad'") return name
Then, use it in your action:
@modal_action(modal_header="Action with Form", form_class=CustomForm) def your_action_with_form(self, request, obj, form_data=None): # Your action logic here return f"Action completed with name: {form_data['name']}"
Testing
To run the tests, execute:
python -m unittest discover django_modal_actions/tests
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_modal_actions-0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4530bcc309c80da4b68f984e4e73178f96c6ea9fa6fd92ef2942e35b602e4946 |
|
MD5 | 29ad4f372a8d3dff960ba9ca211d311d |
|
BLAKE2b-256 | e3cdc29b9dd5141d55c82670d83299b50ab187b8bbba756d62fffbffc114a73a |