在 Django forms.ChoiceField 上设置选定的值 [英] Setting the selected value on a Django forms.ChoiceField
本文介绍了在 Django forms.ChoiceField 上设置选定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是表单中的字段声明:
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,)
我想将初始值设置为 3
但这似乎不起作用.我玩过参数、引号/无引号等......但没有变化.
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.
如果可能的话,有人能给我一个明确的答案吗?和/或对我的代码片段进行必要的调整?
Could anyone give me a definitive answer if it is possible? And/or the necessary tweak in my code snippet?
我使用的是 Django 1.0
推荐答案
在实例化表单时尝试设置初始值:
Try setting the initial value when you instantiate the form:
form = MyForm(initial={'max_number': '3'})
这篇关于在 Django forms.ChoiceField 上设置选定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文