Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么不是';我的Django表单的初始值是否已设置?_Python_Django_Django Forms - Fatal编程技术网

Python 为什么不是';我的Django表单的初始值是否已设置?

Python 为什么不是';我的Django表单的初始值是否已设置?,python,django,django-forms,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)

以下是相关的代码片段

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)
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()})
视图.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)
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()})
首字母是key:value(fieldname/value)对。应该是这样的,

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

好吧,那就更有意义了。我希望有人会说“关键:其他答案中的价值”。谢谢你。我想一个更优秀的程序员会从大括号中找到答案。顺便说一句,forms.py上有没有任何地方我可以设置它,这样我就不必为所有视图重复
initial={'usersegment':'all'}
?我试着像
usersegment=forms.ChoiceField(label=”“,widget=RadioSelect(),choices=usersegment,initial={'usersegment':'all'})那样附加它
,但它不起作用。谢谢。是的,usersegment=forms.ChoiceField(initial='all'label=“”,widget=RadioSelect(),choices=usersegment)