2
下面是相關片段爲什麼我的初始值沒有設置爲我的Django表單?
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()})
確定這使得很多更有意義。我希望有人會說關鍵:在其他SO答案中的價值。感謝那。我想一個更好的程序員會從看大括號中找出它。 – super9 2011-05-02 06:04:37
順便說一句,有沒有在forms.py任何地方我可以設置這個,所以我不必重複'初始= {'usersegment':'所有'}'我所有的意見?我嘗試追加它像'usersegment = forms.ChoiceField(label =「」,widget = RadioSelect(),choices = usersegment,initial = {'usersegment':'all'})'但它不起作用。謝謝。 – super9 2011-05-02 06:08:45
yes,usersegment = forms.ChoiceField(initial ='all'label =「」,widget = RadioSelect(),choices = usersegment) – 2011-05-02 06:20:02