一个简单的表单,其中的ModelChoiceField显示为单选按钮(继承的小部件)。
每次用户选择单选按钮时,我都会使用onchange事件进行POST:
shipping_choice = ShippingChoiceField(
queryset=ShippingMethods.objects.all(),
empty_label=None,
widget=forms.RadioSelect(attrs={
'class': 'order',
'onchange': '$("#shipping_choice").submit()',
})
)我需要有用户选择的想法,实际上是“选择”当页面重新加载的无线电。
添加一个额外的属性'selected‘是行不通的,因为它需要在用户实际首先做出选择时发生。
发布于 2009-07-25 03:35:50
表单有一个initial属性,您可以使用它:
form = YourForm(initial={'shipping_choice': current_choice})然后,我猜你正在使用一个ModelForm,在这种情况下,你应该只传递实例:
form = YourForm(instance=order) # where "order" is your instance...或者,您可能只是不理解表单可以绑定到数据:
if request.method == 'POST':
order_form = YourForm(data=request.POST)
else:
order_form = YourForm()发布于 2009-07-25 08:54:25
不幸的是,你的问题和随后的评论并不清楚。当表单重新提交时显示它是什么意思?
通常流程是显示表单,提交,如果有任何错误则重新显示表单,然后进入确认页面。在什么情况下您会遇到问题?或者你的流程与此有显著的不同?
https://stackoverflow.com/questions/1180745
复制相似问题