mymiracl
mymiracl

Reputation: 579

Confirmation button on save button like delete already has

I want to modify Django admin interface.

There is delete button already has a confirmation page. But I want to add this confirmation to save or change buttons. Actually not exactly the same delete button.

I want to change default admin buttons like this JS or I want to add JS to admin buttons.

<input type="submit" onclick="linkSubmit('http://www.google.com')" value="Submit">
<p id="demo"></p>

<script>
function linkSubmit(link) {
  let text = "Press a button!\nEither OK or Cancel.";
  if (confirm(text) == true) {
    window.location.href = link;
  } else {
  }
  document.getElementById("demo").innerHTML = text;
}
</script>

Upvotes: 1

Views: 563

Answers (3)

mymiracl
mymiracl

Reputation: 579

We found the solution in the files of the delete command. We took copies of the files confirm to the delete function and connected them to the confirm button.

We still can't give it as a alert. Gives confirmation on a another page.

Upvotes: 0

aaron
aaron

Reputation: 43083

  1. Create templates/admin/change_form.html in your project:
{% extends "admin/change_form.html" %}

{% block admin_change_form_document_ready %}{{ block.super }}
<script id="django-admin-form-change-constants"
        data-model-name="{{ opts.model_name }}">
    let modelName = document.getElementById('django-admin-form-change-constants').dataset.modelName;
    let form = document.getElementById(modelName + '_form');
    form.addEventListener('submit', (event) => {
        let text = "Press a button!\nEither OK or Cancel.";
        if (!confirm(text)) {
            event.preventDefault();
        }
    });
</script>
{% endblock %}
  1. Set DIRS in myproject/settings.py to point to your project's templates directory:
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'],
        'APP_DIRS': True,
        ...
    }
]

References:

Upvotes: 0

John Michael Law
John Michael Law

Reputation: 153

Assuming there is already some type of event listener for the button I would add my own custom function as an additional listener for the on click event. Then I would put in my if(confirm) logic and call event.stopImmediatePropagation() as needed to prevent the original functionality from occuring.

Upvotes: 0

Related Questions