我看到没有按钮的表单非常流行(例如here)。在用户选择字段(示例1)或键入文本并单击某些内容(表示完成输入)(示例2)之后,如何创建将自动为Django中两个不同字段提交的表单:
1。)ChoiceField
forms.py
class Search(forms.Form):
field = forms.ChoiceField(choices=MY_CHOICES)
views.py
if request.method == "GET":
form = Search(request.GET)
if form.is_valid():
print('it's work')
template.html
<form method="GET">
{% csrf_token %}
{{ form }}
</form>
2。)CharField
forms.py
class Search(forms.Form):
field = forms.CharField(max_length=10)
* 其他类似上面的文件
任何帮助将不胜感激
答案 0 :(得分:2)
您可以像这样在模板中使用jquery:
$('#search_field').change(function(){
$('#your_form').submit()
});
或当用户单击某些内容时:
$('#something').click(function(){
$('#your_form').submit()
});
答案 1 :(得分:0)
您可以简单地更改 forms.py :
class Search(forms.Form):
field = forms.ChoiceField(choices=MY_CHOICES,
widget=forms.Select(attrs={'onchange': 'submit();'}))
没有什么可添加的,不需要jquery。
另请参阅here。