On my app in django 1.6.5 I am using the django-grappelli admin interface. The admin forms by default have "Save" and "Save and add another" buttons. What is happening, though, is that users sometimes "double click" the save button or they click the save button and then click the "save and add another" before the save has completed. Unfortunately, this seems to execute 2 "save" events on the models and creates duplicate records in the database - though with unique "autoinc" keys. Is there an easy way to prevent the save button from triggering more than once on the default forms?
Asked
Active
Viewed 702 times
1 Answers
0
Well no quick takers. My ugly workaround was to override the grappelli "submit_line.html" template to give ids to the input types:
{% load i18n %}
<footer class="grp-module grp-submit-row grp-fixed-footer">
<header style="display:none"><h1>Submit Options</h1></header>
<ul>
{% if show_delete_link %}
<li class="grp-float-left"><a href="delete/" class="grp-button grp-delete-link">{% trans "Delete" %}</a></li>
{% endif %}
{% if show_save %}
<li><input id="save" type="submit" value="{% trans 'Save' %}" class="grp-button grp-default" name="_save" /></li>
{% endif %}
{% if show_save_as_new %}
<li><input id="savenew" type="submit" value="{% trans 'Save as new' %}" class="grp-button" name="_saveasnew" /></li>
{% endif %}
{% if show_save_and_add_another %}
<li><input id="saveadd" type="submit" value="{% trans 'Save and add another' %}" class="grp-button" name="_addanother" /></li>
{% endif %}
{% if show_save_and_continue %}
<li><input id="savecont" type="submit" value="{% trans 'Save and continue editing' %}" class="grp-button" name="_continue" /></li>
{% endif %}
</ul>
</footer>
I then wrote a (probably badly coded) jquery script to disable the buttons once clicked. Since they all submit the form and it redisplays, this seems to work.
(function($) {
$(document).ready(function() {
$('#save').click(function (e) {
$('#saveadd').click(false);
$('#savecont').click(false);
$('#save').click(false);
})
$('#saveadd').click(function (e) {
$('#saveadd').click(false);
$('#savecont').click(false);
$('#save').click(false);
})
$('#savecont').click(function (e) {
$('#saveadd').click(false);
$('#savecont').click(false);
$('#save').click(false);
})
});
}(django.jQuery));

oldlibmike
- 31
- 3