django-grappelli multiple" save"按钮按下

时间:2014-06-26 13:22:06

标签: django button duplicates django-grappelli

在django 1.6.5中我的应用程序我正在使用django-grappelli管理界面。默认情况下,管理员表单有"保存"和"保存并添加另一个"纽扣。然而,正在发生的事情是,用户有时会双击"保存按钮或单击保存按钮,然后单击"保存并添加另一个"保存完成之前。不幸的是,这似乎执行2" save"模型上的事件并在数据库中创建重复记录 - 尽管有独特的" autoinc"键。有没有一种简单的方法可以防止保存按钮在默认表单上多次触发?

1 个答案:

答案 0 :(得分:0)

没有快速接受者。我丑陋的解决方法是覆盖grappelli" submit_line.html"模板,为输入类型提供ID:

{% 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>
然后我编写了一个(可能是编码很糟糕的)jquery脚本来禁用按钮一旦点击。由于他们都提交表格并重新显示,这似乎有效。

(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));