我正在开发一个基于用户选择的动态表单。这是一个分为两个阶段的过程:首先用户从单选按钮列表中进行选择,然后根据所选内容,将向用户显示适当的表单字段。我知道如何使用type来实现这一点,但我使用了两个不同的表单(1个用于初始选择,1个用于自定义表单字段),以及2个不同的视图。 我有两个问题: 1) 把这两种形式和观点分开是对的吗? 2) 一旦用户填写了第二个(自定义)表单,我如何在运行时知道哪些字段 是为了创建对象并将其保存到数据库中而呈现给她的?在
class SelectionForm(forms.Form):
choice = forms.ModelChoiceField(queryset=Fruit.objects.filter(...)
widget=forms.RadioSelect,
initial='')
# views.py
def review(request):
if request.method == 'POST':
form = SelectionForm(request.POST)
if form.is_valid():
user_choice = form.cleaned_data['choice']
return HttpResponseRedirect('/new_order/%s' % (user_choice))
else:
form = SelectionForm()
return render_to_response( '/new_order.html', {'form': form} )
def order_fruit(request, user_choice):
if request.method == 'POST':
"""
make_order_form uses Python type to create a form
depending on user_choice
"""
form = make_order_form(request.POST, user_choice)
if form.is_valid():
# How to know which fields were presented to the user -
# in order to create the Order object with the right -
# arguments ????????????????????
return HttpResponseRedirect('/thanks/')
else:
form = make_order_form(request, user_choice)
return render_to_response('/second_step_order.html', { 'form':form })
反省一下你的状态。看看里面'窗体字段'或'已清理的数据‘. 在
相关问题 更多 >
编程相关推荐