Skip to content

Commit 69428bd

Browse files
authored
[feature] Made admin/submit_line.html extendable
1 parent 7a093b2 commit 69428bd

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

docs/developer/admin-utilities.rst

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
{% load i18n admin_urls %}
22
<div class="submit-row">
3+
{% block submit-row %}
34
{% if show_save %}<input type="submit" value="{% trans 'Save' %}" class="default" name="_save" />{% endif %}
45
{% if show_delete_link %}
56
{% url opts|admin_urlname:'delete' original.pk|admin_urlquote as delete_url %}
67
<p class="deletelink-box"><a href="{% add_preserved_filters delete_url %}" class="deletelink">{% trans "Delete" %}</a></p>
78
{% endif %}
89
{% if additional_buttons %}
910
{% for button in additional_buttons %}
11+
{% if button.raw_html %}
12+
{{ button.raw_html }}
13+
{% else %}
1014
<input type="{{ button.type }}"
1115
data-url="{{ button.url }}"
1216
class="{{ button.class }}"
1317
value="{{ button.value }}"
1418
title="{{ button.title }}">
19+
{% endif %}
1520
{% endfor %}
1621
{% endif %}
1722
{% if show_save_and_continue %}<input type="submit" value="{% trans 'Save and continue editing' %}" name="_continue" />{% endif %}
1823
{% if show_save_as_new %}<input type="submit" value="{% trans 'Save as new' %}" name="_saveasnew" />{% endif %}
1924
{% if show_save_and_add_another %}<input type="submit" value="{% trans 'Save and add another' %}" name="_addanother" />{% endif %}
25+
{% endblock %}
2026
</div>

0 commit comments

Comments
 (0)