我希望得到您对我的问题的帮助。通常,当我使用提交按钮在 Django
中提交 POST 请求
时,我会采用如下所示的按钮名称:
<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>
在 View 中
if "submitButton" in request.POST:
关于javascript - request.POST 从 Django 使用 Javascript 提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53080786/