在Django forms.ChoiceField上设置所选值 [英] Setting the selected value on a Django forms.ChoiceField
问题描述
这是一个表单中的字段声明:
max_number = forms.ChoiceField(widget = forms.Select()
choices =([('1','1'),('2','2'),('3','3'),]),initial ='3',required = True ,)
我想将初始值设置为3,这似乎不起作用。我已经玩过param,quote / no quotes等...但没有改变。
通过Google发现的一些结果表明可以设置初始值,但我从来没有设法让它工作!我在django 1.0的方式。
有可能有人给我一个确定的答案吗?和/或我的代码片段中必要的调整?
非常感谢。
form = MyForm(initial = {'max_number': '3'})
Here is the field declaration in a form:
max_number = forms.ChoiceField(widget = forms.Select(),
choices = ([('1','1'), ('2','2'),('3','3'), ]), initial='3', required = True,)
I would like to set the initial value to be 3 and this doesn't seem to work. I have played about with the param, quotes/no quotes etc... but no change.
A few results I've found through Google suggest it is possible to set the initial value but I've never ever managed to get it to work! I am on django 1.0 by the way.
Could anyone give me a definitive answer if it is possible? And/or the necessary tweak in my code snippet?
Many thanks.
Try setting the initial value when you instantiate the form:
form = MyForm(initial={'max_number': '3'})
这篇关于在Django forms.ChoiceField上设置所选值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!