diff --git a/README.md b/README.md index 307eb2eae..407086ecb 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,7 @@ Did you decide to start using Unfold but you don't have time to make the switch - [Custom unfold @action decorator](#custom-unfold-action-decorator) - [Action handler functions](#action-handler-functions) - [Action examples](#action-examples) + - [Action with form example](#action-with-form-example) - [Filters](#filters) - [Text filters](#text-filters) - [Dropdown filters](#dropdown-filters) @@ -466,6 +467,63 @@ class UserAdmin(ModelAdmin): ) ``` +### Action with form example + +Below is an example of an action that will display a form after clicking on the action button on the detail object page. + +```python +from django import forms +from django.template.loader import render_to_string +from django.urls import reverse_lazy + +from unfold.widgets import UnfoldAdminTextInputWidget + + +class SomeForm(forms.Form): + # It is important to set a widget coming from Unfold + note = forms.CharField(label=_("Note"), widget=UnfoldAdminTextInputWidget) + + +@register(User) +class UserAdmin(ModelAdmin): + actions_detail = ["change_detail_action_block"] + form = SomeForm(request.POST or None) + + @action(description=_("Detail")) + def change_detail_action_block(self, request: HttpRequest, object_id: int) -> str: + user = User.objects.get(pk=object_id) + + if request.method == "POST" and form.is_valid(): + # Do something with form data + form.cleaned_data["note"] + + return redirect( + reverse_lazy("admin:users_user_change", args=[object_id]) + ) + + return render_to_string("some/template.html", { + "form": form, + }) +``` + +Template displaying the form. Please note that breadcrumbs are empty in this case but if you want, you can configure your own breadcrumbs path. + +```html +{% extends "admin/base_site.html" %} + +{% block breadcrumbs %}{% endblock %} + +{% block content %} +
+{% endblock %} +``` + ## Filters By default, Django admin handles all filters as regular HTML links pointing at the same URL with different query parameters. This approach is for basic filtering more than enough. In the case of more advanced filtering by incorporating input fields, it is not going to work.