Last active
May 13, 2024 20:38
-
-
Save azimjohn/adbfe745689d47972cfce28fed641866 to your computer and use it in GitHub Desktop.
Django Admin Action Confirmation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% extends "admin/base_site.html" %} | |
{% load i18n l10n admin_urls %} | |
{% block bodyclass %}{{ block.super }} app-{{ opts.app_label }} model-{{ opts.model_name }} delete-confirmation | |
delete-selected-confirmation{% endblock %} | |
{% block content %} | |
<p>Are you sure you want to {{ action }}?</p> | |
<ul style="padding: 0"> | |
{% for object in queryset.all %} | |
<li style="list-style: none; float: left; margin: 5px"> | |
{{ object }} | |
</li> | |
{% endfor %} | |
</ul> | |
<hr> | |
<br> | |
<form action="" method="post">{% csrf_token %} | |
<fieldset class="module aligned"> | |
{% for obj in queryset.all %} | |
<input type="hidden" name="_selected_action" value="{{ obj.pk|unlocalize }}"/> | |
{% endfor %} | |
</fieldset> | |
<div class="submit-row"> | |
<input type="hidden" name="action" value="{{ action }}"/> | |
<input type="submit" name="confirmation" value="Confirm"/> | |
<a href="#" onclick="window.history.back(); return false;" | |
class="button cancel-link">{% trans "No, take me back" %}</a> | |
</div> | |
</form> | |
{% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# decorator for model action | |
def require_confirmation(func): | |
def wrapper(modeladmin, request, queryset): | |
if request.POST.get("confirmation") is None: | |
request.current_app = modeladmin.admin_site.name | |
context = { | |
"action": request.POST["action"], | |
"queryset": queryset, | |
} | |
return TemplateResponse(request, "admin/action_confirmation.html", context) | |
return func(modeladmin, request, queryset) | |
wrapper.__name__ = func.__name__ | |
return wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍