我正在使用帶有脆皮表單的Django,並且還使用Shopify嵌入式應用SDK。我試圖讓模式窗口看起來讓用戶確認他們想要刪除一個對象。Djquery Modal對Django表單的確認提交刪除對象
我的代碼已附加到目前爲止。我有下面的代碼出現的模態窗口,但是,形式不後提交(和對象沒有被刪除)模態窗口的用戶選擇「刪除」:
它只是關閉模式沒有任何反應。
我已經嘗試過各種方法從網上,但沒有任何運氣。
form.py使用Shopify嵌入式應用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})
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 =「按鈕」'並點擊它將不會執行任何操作。 – xyres
@xyres我之前嘗試過這一點,並在迴應菲利普下面,但一直沒有得到所需的行爲... – cj1689262