|
| 1 | +# Django Modal Actions |
| 2 | + |
| 3 | +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. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Easy integration with Django admin |
| 8 | +- Support for both list-view and object-view actions |
| 9 | +- Customizable modal forms |
| 10 | +- AJAX-based form submission |
| 11 | + |
| 12 | +## Requirements |
| 13 | + |
| 14 | +- Python (>= 3.7) |
| 15 | +- Django (>= 3.2) |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +1. Install the package using pip: |
| 20 | + |
| 21 | + ``` |
| 22 | + pip install django-modal-actions |
| 23 | + ``` |
| 24 | + |
| 25 | +2. Add `'django_modal_actions'` to your `INSTALLED_APPS` setting: |
| 26 | + |
| 27 | + ```python |
| 28 | + INSTALLED_APPS = [ |
| 29 | + ... |
| 30 | + 'django_modal_actions', |
| 31 | + ... |
| 32 | + ] |
| 33 | + ``` |
| 34 | + |
| 35 | +## Usage |
| 36 | + |
| 37 | +1. In your `admin.py`, import the necessary components: |
| 38 | + |
| 39 | + ```python |
| 40 | + from django.contrib import admin |
| 41 | + from django_modal_actions import ModalActionMixin, modal_action |
| 42 | + ``` |
| 43 | + |
| 44 | +2. Create a custom admin class that inherits from `ModalActionMixin` and your base admin class: |
| 45 | + |
| 46 | + ```python |
| 47 | + @admin.register(YourModel) |
| 48 | + class YourModelAdmin(ModalActionMixin, admin.ModelAdmin): |
| 49 | + modal_actions = ["your_object_action"] |
| 50 | + list_modal_actions = ["your_list_action"] |
| 51 | + |
| 52 | + @modal_action(modal_header="Object Action") |
| 53 | + def your_object_action(self, request, obj, form_data=None): |
| 54 | + # Your object action logic here |
| 55 | + return "Action completed successfully" |
| 56 | + |
| 57 | + @modal_action(modal_header="List Action") |
| 58 | + def your_list_action(self, request, queryset, form_data=None): |
| 59 | + # Your list action logic here |
| 60 | + return f"Action completed on {queryset.count()} items" |
| 61 | + ``` |
| 62 | + |
| 63 | +3. If you need a custom form for your action, create a form class: |
| 64 | + |
| 65 | + ```python |
| 66 | + from django import forms |
| 67 | + |
| 68 | + class CustomForm(forms.Form): |
| 69 | + name = forms.CharField(label="Name", required=True) |
| 70 | + |
| 71 | + def clean_name(self): |
| 72 | + name = self.cleaned_data["name"] |
| 73 | + if name == "bad": |
| 74 | + raise forms.ValidationError("Name cannot be 'bad'") |
| 75 | + return name |
| 76 | + ``` |
| 77 | + |
| 78 | + Then, use it in your action: |
| 79 | + |
| 80 | + ```python |
| 81 | + @modal_action(modal_header="Action with Form", form_class=CustomForm) |
| 82 | + def your_action_with_form(self, request, obj, form_data=None): |
| 83 | + # Your action logic here |
| 84 | + return f"Action completed with name: {form_data['name']}" |
| 85 | + ``` |
| 86 | + |
| 87 | +## Testing |
| 88 | + |
| 89 | +To run the tests, execute: |
| 90 | + |
| 91 | +``` |
| 92 | +python -m unittest discover django_modal_actions/tests |
| 93 | +``` |
| 94 | + |
| 95 | +## Contributing |
| 96 | + |
| 97 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 98 | + |
| 99 | +## License |
| 100 | + |
| 101 | +This project is licensed under the MIT License. |
0 commit comments