In my Django app I want to implement an intermediate page which asks for confirmation before executing a specific admin action. I took this post as an example.
I used the existing delete_confirmation.html
template as a departing point and partially got it working. The confirmation page is shown and the selected objects are displayed. However my admin action is never called after clicking "Yes, I'm sure".
In my admin.py I have:
def cancel_selected_bookings(self, request, queryset):
"""
Cancel selected bookings.
"""
if request.POST.get("post"):
for booking in queryset:
booking.cancel()
message = "Booking %s successfully cancelled." % booking.booking_id
messages.info(request, message)
else:
context = {
"objects_name": "bookings",
'title': "Confirm cancellation of selected bookings:",
'cancellable_bookings': [queryset],
'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME,
}
return TemplateResponse(request, 'admin/bookings/confirm_cancel.html', context, current_app=self.admin_site.name)
In my template I have (a clipping of the full template):
<div class="grp-group">
<h2>
{% blocktrans %}
Are you sure you want to cancel the selected {{ objects_name }}?
{% endblocktrans %}
</h2>
{% for cancellable_booking in cancellable_bookings %}
<ul class="grp-nested-list">{{ cancellable_booking|unordered_list }}</ul>
{% endfor %}
</div>
<form action="" method="post">{% csrf_token %}
<div id="submit" class="grp-module grp-submit-row grp-fixed-footer">
{% for obj in queryset %}
<input type="hidden" name="{{ action_checkbox_name }}" value="{{ obj.pk|unlocalize }}" />
{% endfor %}
<input type="hidden" name="action" value="cancel_selected_bookings" />
<input type="hidden" name="post" value="yes" />
<ul>
<li class="grp-float-left"><a href="." class="grp-button grp-cancel-link">{% trans "Cancel" %}</a></li>
<li><input type="submit" value="{% trans "Yes, I'm sure" %}" class="grp-button grp-default" /></li>
</ul>
<input type="hidden" name="post" value="yes" />
</div>
</form>
EDIT:
I found one problem in the above template. The lines:
{% for obj in queryset %}
<input type="hidden" name="{{ action_checkbox_name }}" value="{{ obj.pk|unlocalize }}" />
{% endfor %}
need to be replaced by:
{% for cancellable_booking in cancellable_bookings %}
<input type="hidden" name="{{ action_checkbox_name }}" value="{{ cancellable_booking.id }}" />
{% endfor %}
For some mysterious reason however, the value of the hidden fields isn't being set by {{cancellable_booking.id}}
. When I hard code that with an existing id it all works as expected. What am I doing wrong??