Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Admin Action Confirmation Page

In my Django project I have an admin action which requires an confirmation page. I oriented it on the delete_selected action, but it does not work. Here is what I have.

part of my admin.py

def my_action(modeladmin, request, queryset):
    if request.POST.get('post'):
        print "Performing action"
        # action code here
        return None
    else:
        return TemplateResponse(request, "admin/my_action_confirmation.html")

admin/my_action_confirmation.html

<form action="" method="post">{% csrf_token %}
    <div>
        <input type="hidden" name="post" value="yes" />
        <input type="hidden" name="action" value="my_action" />
        <input type="submit" value="Confirm" />
    </div>
</form>

This works almost. I get to the confirmation page but if I click "confirm" I just get back to the original page. The part with the action code is never reached. In fact the my_action function isn't called a second time. So how do I tell django, that the my_action function should be called a second time, once I clicked confirm?

like image 288
mogoh Avatar asked May 05 '17 13:05

mogoh


2 Answers

Edit: I was more missing then I thought

The corrected my_action

def my_action(modeladmin, request, queryset):
    if request.POST.get('post'):
        print "Performing action"
        # action code here
        return None
    else:
        request.current_app = modeladmin.admin_site.name
        return TemplateResponse(request, "admin/my_action_confirmation.html")

admin/my_action_confirmation.html

{% load l10n %}
<form action="" method="post">{% csrf_token %}
    <div>
        {% for obj in queryset %}
        <input type="hidden" name="{{ action_checkbox_name }}" value="{{ obj.pk|unlocalize }}" />
        {% endfor %}
        <input type="hidden" name="post" value="yes" />
        <input type="hidden" name="action" value="my_action" />
        <input type="submit" value="Confirm" />
    </div>
</form>
like image 80
mogoh Avatar answered Sep 26 '22 07:09

mogoh


admin.py

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

@require_confirmation
def do_dangerous_action(modeladmin, request, queryset):
    return 42/0

admin/action_confirmation.html

{% 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 %}
like image 20
Azimjon Pulatov Avatar answered Sep 26 '22 07:09

Azimjon Pulatov