@@ -218,3 +218,63 @@ all at once which may cause the slow loading of the page.
218218 To customize or know more about it, please refer to the
219219`django-admin-autocomplete-filter documentation
220220<https://github.com/farhan0581/django-admin-autocomplete-filter#usage> `_.
221+
222+ Customizing the Submit Row in OpenWISP Admin
223+ --------------------------------------------
224+
225+ In the OpenWISP admin interface, the ``submit_line.html `` template
226+ controls the rendering of action buttons in the model form's submit row.
227+ OpenWISP Utils extends this template to allow the addition of custom
228+ buttons.
229+
230+ To add custom buttons, you can use the ``additional_buttons `` context
231+ variable. This variable should be a list of dictionaries, each
232+ representing a button with customizable properties such as type, class,
233+ value, title, URL, or even raw HTML content.
234+
235+ Here's an example of adding a custom button with both standard properties
236+ and raw HTML to the submit row in the ``change_view `` method:
237+
238+ .. code-block :: python
239+
240+ from django.contrib import admin
241+ from django.utils.safestring import mark_safe
242+ from .models import MyModel
243+
244+
245+ @admin.register (MyModel)
246+ class MyModelAdmin (admin .ModelAdmin ):
247+ def change_view (
248+ self , request , object_id , form_url = " " , extra_context = None
249+ ):
250+ extra_context = extra_context or {}
251+ extra_context[" additional_buttons" ] = [
252+ {
253+ " type" : " button" ,
254+ " class" : " btn btn-secondary" ,
255+ " value" : " Custom Action" ,
256+ " title" : " Perform a custom action" ,
257+ " url" : " https://example.com" ,
258+ },
259+ {
260+ " raw_html" : mark_safe(
261+ ' <button type="button" class="btn btn-warning" '
262+ " onclick=\" alert('This is a raw HTML button!')\" >"
263+ " Raw HTML Button</button>"
264+ )
265+ },
266+ ]
267+ return super ().change_view(
268+ request, object_id, form_url, extra_context
269+ )
270+
271+ In this example, two buttons are added to the submit row:
272+
273+ 1. A standard button labeled "Custom Action" with a link to
274+ `https://example.com `.
275+ 2. A button rendered using raw HTML that triggers an alert when clicked,
276+ labeled "Raw HTML Button." The raw HTML is wrapped in `mark_safe ` to
277+ ensure it is rendered correctly.
278+
279+ The `mark_safe ` function is necessary to ensure that the raw HTML is
280+ rendered as HTML and not escaped as plain text.
0 commit comments