|
| 1 | +--- |
| 2 | +title: Action with form example |
| 3 | +description: Actions containing custom form example. |
| 4 | +order: 5 |
| 5 | +--- |
| 6 | + |
| 7 | +# Action with form example |
| 8 | + |
| 9 | +Below is an example of an action that will display a form after clicking on the action button on the detail object page. |
| 10 | + |
| 11 | +```python |
| 12 | +# admin.py |
| 13 | + |
| 14 | +from django import forms |
| 15 | +from django.contrib.auth.models import User |
| 16 | +from django.http import HttpRequest |
| 17 | +from django.shortcuts import render |
| 18 | +from django.urls import reverse_lazy |
| 19 | +from django.utils.translation import gettext_lazy as _ |
| 20 | + |
| 21 | +from unfold.admin import ModelAdmin |
| 22 | +from unfold.decorators import action |
| 23 | +from unfold.widgets import UnfoldAdminTextInputWidget, UnfoldAdminSplitDateTimeWidget |
| 24 | + |
| 25 | + |
| 26 | +class SomeForm(forms.Form): |
| 27 | + # It is important to set a widget coming from Unfold |
| 28 | + date_start = forms.SplitDateTimeField(label=_("Start"), widget=UnfoldAdminSplitDateTimeWidget) |
| 29 | + date_end = forms.SplitDateTimeField(label=_("End"), widget=UnfoldAdminSplitDateTimeWidget) |
| 30 | + note = forms.CharField(label=_("Note"), widget=UnfoldAdminTextInputWidget) |
| 31 | + |
| 32 | + # Loads date widget required JS files |
| 33 | + class Media: |
| 34 | + js = [ |
| 35 | + "admin/js/vendor/jquery/jquery.js", |
| 36 | + "admin/js/jquery.init.js", |
| 37 | + "admin/js/calendar.js", |
| 38 | + "admin/js/admin/DateTimeShortcuts.js", |
| 39 | + "admin/js/core.js", |
| 40 | + ] |
| 41 | + |
| 42 | + |
| 43 | +@register(User) |
| 44 | +class UserAdmin(ModelAdmin): |
| 45 | + actions_detail = ["change_detail_action"] |
| 46 | + |
| 47 | + @action(description=_("Change detail action"), url_path="change-detail-action") |
| 48 | + def change_detail_action(self, request: HttpRequest, object_id: int) -> str: |
| 49 | + # Check if object already exists, otherwise returs 404 |
| 50 | + obj = get_object_or_404(User, pk=object_id) |
| 51 | + form = SomeForm(request.POST or None) |
| 52 | + |
| 53 | + if request.method == "POST" and form.is_valid(): |
| 54 | + # Process form data |
| 55 | + # form.cleaned_data["note"] |
| 56 | + # form.cleaned_data["date_from"] |
| 57 | + # form.cleaned_data["date_to"] |
| 58 | + |
| 59 | + messages.success(request, _("Change detail action has been successful.")) |
| 60 | + |
| 61 | + return redirect( |
| 62 | + reverse_lazy("admin:app_model_change", args=[object_id]) |
| 63 | + ) |
| 64 | + |
| 65 | + return render( |
| 66 | + request, |
| 67 | + "some/action.html", |
| 68 | + { |
| 69 | + "form": form, |
| 70 | + "object": obj, |
| 71 | + "title": _("Change detail action for {}").format(obj), |
| 72 | + **self.admin_site.each_context(request), |
| 73 | + }, |
| 74 | + ) |
| 75 | +``` |
| 76 | + |
| 77 | +Template displaying the form. Please note that breadcrumbs are empty in this case but if you want, you can configure your own breadcrumbs path. |
| 78 | + |
| 79 | +```html |
| 80 | +{% extends "admin/base_site.html" %} |
| 81 | + |
| 82 | +{% load i18n unfold %} |
| 83 | + |
| 84 | +{% block breadcrumbs %}{% endblock %} |
| 85 | + |
| 86 | +{% block extrahead %} |
| 87 | + {{ block.super }} |
| 88 | + <script src="{% url 'admin:jsi18n' %}"></script> |
| 89 | + {{ form.media }} |
| 90 | +{% endblock %} |
| 91 | + |
| 92 | +{% block content %} |
| 93 | + <form action="" method="post" novalidate> |
| 94 | + <div class="aligned border border-gray-200 mb-8 rounded-md pt-3 px-3 shadow-sm dark:border-gray-800"> |
| 95 | + {% csrf_token %} |
| 96 | + |
| 97 | + {% for field in form %} |
| 98 | + {% include "unfold/helpers/field.html" with field=field %} |
| 99 | + {% endfor %} |
| 100 | + </div> |
| 101 | + |
| 102 | + <div class="flex justify-end"> |
| 103 | + {% component "unfold/components/button.html" with submit=1 %} |
| 104 | + {% trans "Submit form" %} |
| 105 | + {% endcomponent %} |
| 106 | + </div> |
| 107 | + </form> |
| 108 | +{% endblock %} |
| 109 | +``` |
0 commit comments