我正在使用带有Crispy表单的Django,也正在使用Shopify Embedded Apps SDK。我正在尝试显示一个模式窗口,以使用户确认他们要删除对象。
到目前为止,我的代码已附加。我在模态窗口中显示以下代码,但是,在用户在模态窗口中选择“删除”后,表单未提交(并且未删除对象):
它只是关闭了模态,什么也没有发生。
我从网上尝试了各种方法,但是没有运气。
表格
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
@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})
使用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>
渲染的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>
参考方案
您的删除按钮(<input type="button" name="delete"...
)永远不会发送。
您进行调试的一种快速方法是在视图中检查django中的request.POST变量。
在这里,您以编程方式提交表单,因此“删除”永远不会收到。
else {
alert("Contine");
$("form#edit_form").submit();
}
我建议在提交表单之前通过javascript将一个字段添加到表单中:
else {
alert("Continue");
$('<input>').attr({
type: 'hidden',
name: 'delete'
value: 'delete'
}).appendto('#edit_form');
$("form#edit_form").submit();
}
或将您的delete
按钮设置为<input type="submit"...
,以便随表格一起发送。
我想在JavaScript中使用模板变量:我的问题是在javascript代码中使用for循环,for循环之间的所有事情都会重复..但我不想要....下面粘贴了我的代码..有人可以告诉我更好的方法吗这..因为这看起来很丑..这是我的代码: {% block extra_javascript %} <script src="/static/js…
使用JS和PHP更改弹出窗口背景图像 - javascript我有一个JS函数:function zoom(now) { document.getElementById("popup").style.display = "block"; document.getElementById("photos").style.backgroundImage = …
打印二维阵列 - javascript我正在尝试打印子元素。在this example之后。怎么做?。$myarray = array("DO"=>array('IDEAS','BRANDS','CREATIVE','CAMPAIGNS'), "JOCKEY"=>a…
获取JavaScript值到C#字符串 - javascript是否可以在C#中执行类似的操作?该值为“ 10/05/2014”string jsValue = javascript("$('#EstimatedStartDate').val()"); 参考方案 您能否更详细地阐明您要做什么。看来您正在尝试从javascript(客户…
执行onclick时获得意外令牌 - javascript我正在使用onclick事件从PHP调用JS函数。这是我的代码:我在一个函数中,因此我需要通过PHP来完成它,因为然后我会返回:$html = '<input type="checkbox" checked value="1" id="setGetSku" name="se…