2

django 1.6.5 の私のアプリでは、django-grappelli 管理インターフェイスを使用しています。デフォルトでは、管理フォームには「保存」ボタンと「保存して別のものを追加」ボタンがあります。ただし、ユーザーが保存ボタンを「ダブルクリック」するか、保存ボタンをクリックしてから保存が完了する前に「保存して別のものを追加」をクリックすることがあります。残念ながら、これはモデルで 2 つの「保存」イベントを実行し、データベースに重複レコードを作成するように見えますが、一意の「autoinc」キーを使用します。デフォルトのフォームで保存ボタンが複数回トリガーされるのを防ぐ簡単な方法はありますか?

4

1 に答える 1

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));
于 2014-06-27T02:29:08.233 に答える