我想在我的问题上得到你的帮助。通常,当我使用提交按钮在POST request
中提交Django
时,我会按下这样的按钮名称:
<button id="document-button" type="submit" name="UpdateDocument">Submit</button>
在我看来:
if "UpdateDocument" in request.POST:
... do something
但在我的代码中,我需要使用Javascript onchange
方法提交我的表单。因此,我没有任何按钮来提交我的表格:
<form id="select-animal-form" action="" method="POST">
{% csrf_token %}
<fieldset>
<legend><span class="name">Select Animal</span></legend>
{{ form.animal_list }}
</fieldset>
</form>
<script>
$('#select-animal-form').on('change', function(){
$(this).submit();
});
</script>
我想拿起提交request.POST
这张表格。我写了这个:
if request.POST and not "UpdateDocument" in request.POST:
... do something
但是在我的分页中甚至按钮Next
或Previous
也被称为这一部分。我只想在这部分中提交提交表格。
我怎么能这样做?
谢谢
您可以在表单中添加隐藏输入,如下所示,并检查request.POST
中是否存在
<form id="select-animal-form" action="" method="POST">
{% csrf_token %}
<fieldset>
<legend><span class="name">Select Animal</span></legend>
{{ form.animal_list }}
</fieldset>
<input type="hidden" name="submitButton">
</form>
<script>
$('#select-animal-form').on('change', function(){
$(this).submit();
});
</script>
在视野中
if "submitButton" in request.POST: