![](/img/trans.png)
[英]How to send javascript list from template to request.POST (django framework)
[英]request.POST from Django with Javascript submit form
我想在我的问题上寻求您的帮助。 通常,当我使用提交按钮在Django
提交POST request
时,我会采用这样的按钮名称:
<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:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.