[英]request.POST from Django with Javascript submit form
I would like to get your help on my issue. 我想在我的问题上寻求您的帮助。 Usually, when I submit a
POST request
in Django
with a submit button, I take the button name like this : 通常,当我使用提交按钮在
Django
提交POST request
时,我会采用这样的按钮名称:
<button id="document-button" type="submit" name="UpdateDocument">Submit</button>
And in my view : 在我看来:
if "UpdateDocument" in request.POST:
... do something
But in my code, I need to submit my form with Javascript onchange
method. 但是在我的代码中,我需要使用Javascript
onchange
方法提交表单。 Hence, I don't have any button to submit my form : 因此,我没有任何按钮可以提交表单:
<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>
I would like to pick up the submit request.POST
for this form. 我想领取这个表格的
request.POST
。 I wrote this : 我写了这个:
if request.POST and not "UpdateDocument" in request.POST:
... do something
But even buttons Next
or Previous
in my pagination are called in this part. 但是在这部分中甚至调用了我分页中的“
Next
或“ Previous
按钮。 I just want to call in this part the submit form. 我只想在此部分中提交表单。
How I can do that ? 我该怎么做?
Thank you 谢谢
You can add a hidden input inside the form as below and check if that exist in request.POST
您可以在表单内添加一个隐藏的输入,如下所示,并检查
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>
in view 鉴于
if "submitButton" in request.POST:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.