简体   繁体   English

Django表单上的Jquery模式确认提交删除对象

[英]Jquery Modal Confirmation on Django form submit for deletion of object

I am using Django with Crispy forms and am also using the Shopify Embedded Apps SDK. 我正在使用带有Crispy表单的Django,也正在使用Shopify Embedded Apps SDK。 I am trying to have a modal window appear to let the user confirm that they want to delete an object. 我正在尝试显示一个模式窗口,以使用户确认他们要删除对象。

My code is attached so far. 到目前为止,我的代码已附加。 I have the modal window appearing with the following code, however, the form is not submitted (and the object is not deleted) after the user selects 'delete' on the modal window: 我在模态窗口中显示以下代码,但是,在用户在模态窗口中选择“删除”后,表单未提交(并且未删除对象): 在此处输入图片说明

It just closed the modal and nothing happens. 它只是关闭了模态,什么也没发生。

I have tried various methods from around the net, but haven't had any luck. 我从网上尝试了各种方法,但是没有运气。

form.py 表格

class MyForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)

        self.helper = FormHelper(self)

        self.helper.form_id = 'edit_form'

        self.helper.layout = Layout(        
            [... some input fields...]
        )

        # Add delete button
        self.helper.layout.append(Button('delete', 'delete'))

        # Normal submit button
        self.helper.layout.append(Submit('save', 'save'))

views.py views.py

@login_required
def edit_object(request, *args, **kwargs):
    # Form handling logic
     with request.user.session:
         object = get_object_or_404(models.Object, pk=kwargs['object_id'], ...)

        if request.method == "POST":
            form = forms.MyForm(request.POST, request=request, instance=object)

            if not form.is_valid():
               [... do some stuff ...]

            if form.is_valid():

                # If the save button is pressed
                if request.POST.get('save'):
                    [... do some stuff to save and redirect to url of my choice ...]

                # If the delete button is pressed <<<< The Modal should appear prior to this
                if request.POST.get('delete'):
                    [... delete to object and redirect to url of my choice ... ]
        else:
            form = forms.MyForm(request=request, instance=supplier)

        return render(request, 'template.html', {'form': form})

template.html using Shopify Embedded App SDK: 使用Shopify嵌入式应用程序SDK的template.html:

    <script type="text/javascript">
        [...]
        ShopifyApp.ready(function() {
            ShopifyApp.Bar.initialize({});

            $("#button-id-delete").click(function(ev){
                ShopifyApp.Modal.confirm({message: "Are you sure you want to delete?"}, function(result){
                    if(!result){
                        result.preventDefault();
                    }
                    else {
                        alert("Contine");
                        $("form#edit_form").submit();
                    }
                });
            });
        });
    </script>

<form enctype="multipart/form-data" method="post">
    {% crispy form %}
</form>

rendered html 渲染的HTML

<form enctype="multipart/form-data" method="post">
    <form  id="edit_form" method="post" > <input type='hidden' name='csrfmiddlewaretoken' value='.......' /> 
       [.... various input fields .....]
       <input type="button"
          name="delete"
          value="delete"
          class="btn destroy btn"
          id="button-id-delete"
          />
       <input type="submit"
          name="save"
          value="save"
          class="btn btn-primary"
          id="submit-id-save"
          />
    </form>
</form>

Your delete button ( <input type="button" name="delete"... ) never gets sent. 您的删除按钮( <input type="button" name="delete"... )永远不会发送。

A quick way for you to have debugged this would have been to examined the request.POST variable in django in your view. 一种快速调试此问题的方法是,在您的视图中检查django中的request.POST变量。

Here you are submitting the form programmatically and so the "delete" never gets received. 在这里,您以编程方式提交表单,因此“删除”从未收到。

                else {
                    alert("Contine");
                    $("form#edit_form").submit();
                }

I'd recommend either adding a field to the form via javascript before it gets submitted: 我建议您在提交表单之前通过javascript将一个字段添加到表单中:

                else {
                    alert("Continue");
                    $('<input>').attr({
                        type: 'hidden',
                        name: 'delete'
                        value: 'delete'
                    }).appendto('#edit_form');
                    $("form#edit_form").submit();
                }

Or set your delete button to <input type="submit"... so it gets sent with the form. 或将您的delete按钮设置为<input type="submit"...以便将其与表单一起发送。

I was able to get it working with this js: 我能够使用此js进行操作:

$("#submit-id-delete").click(function(ev){
    ev.preventDefault();

    var deleteBtn = $(this);

    ShopifyApp.Modal.confirm(
        {
            title: "Delete?",
            message: "Are you sure you want to delete this?",
            okButton: "Yes, delete",
            style: "danger"
        },
            function(result){

        if(!result){
            return false;
        }
        else {
            deleteBtn.parents('form').append('<input type="hidden" name="delete" id="delete" value="delete" />').submit();
            return true;
        }

    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM