为什么我的Django表单没有设置初始值?

时间:2011-05-02 05:43:20

标签: python django django-forms

以下是相关摘录

forms.py

usersegment = [['non-paying','Non-paying (Coming soon)'],['paying','Paying (Coming soon)'], ['all', 'All']]

class SegmentForm(forms.Form):
    usersegment = forms.ChoiceField(label="", widget=RadioSelect(), choices=usersegment)

views.py

if request.method == 'GET' and 'datestart' in request.GET:

    return render_to_response('activation/activation_signupcount.html', 
                            {   'datestart': request.GET['datestart'], 
                                'dateend': request.GET['dateend'], 
                                'usersegment': request.GET['usersegment'], 
                                'form': SegmentForm(request.GET), 
                                'form2': DateSelectForm(request.GET)    })
else:

    return render_to_response('activation/activation_signupcount.html', 
                            {'form': SegmentForm(initial={'all':'All'}), 'form2': DateSelectForm()})

1 个答案:

答案 0 :(得分:3)

首字母是关键:值(字段名/值)对。它应该是这样的,

else:
    return render_to_response('activation/activation_signupcount.html', 
              {'form': SegmentForm(initial={'usersegment':'all'}), 
               'form2': DateSelectForm()
              })