忽略django表单选择字段的初始值 [英] initial value for django form choice field ignored
问题描述
class UserUsesSourceForm(forms.Form):
#some fields here
username = forms.CharField(label =(Username),max_length = 30,help_text =(Required))
provider = forms.ChoiceField(widget = forms.Select(),choices = SOURCES_CHOICES,初始= SOURCES_CHOICES [1])$ b $ b
可用的选项是:
E ='e'
A ='a'
SOURCES_CHOICES =(
(A,'A'),
(E,'E'),
)
视图:
form = UserUsesSourceForm(initial = {username:request.user.username,'provider':SOURCES_CHOICES [1]})return render_to_response ('update_datasource.html',context_instance = RequestContext(request,params))
模板: / p>
< form action =method =post>
{%csrf_token%}
{%if form.non_field_errors%}
< p>
{%for form.non_field_errors%}
< div class =text-error> {{error | escape}}< / div>
{%endfor%}
< / p>
{%endif%}
< div class =control-group>
< label class =control-labelfor =id_provider>数据源< / label>
< div class =controls>
{{form.provider}}
< / div>
< / div>
< / form>
问题是,即使初始值正确设置,我可以在debug中进行测试即,表单提供者字段的初始值是我想要的元组),最终的HTML总是显示选择框中的第一个元素:
< select name =providerid =id_provider>
< option value =A> A< / option>
< option value =E> E< / option>
< / select>
..而我希望它有一个默认或活动选项。
请注意,用户名字段已正确初始化。
如何进一步调查以确定问题在哪里?
您需要传递 I have this form: The available choices are: The view: And the template: The problem is that even if the initial value is correctly set, and I can test it in debug (i.e., the form "provider" field initial value is the tuple I want), the final html always show the first element in the select box: ..while I'd expect it to have a "default" or "active" option.
Please note that the username field is correctly initialized.
How can I investigate further to find out where the problem is? You need to pass the
这篇关于忽略django表单选择字段的初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! form = UserUsesSourceForm(
initial = {'username':request.user.username,
'provider':SOURCES_CHOICES [1] [0]})
class UserUsesSourceForm(forms.Form):
# some fields here
username = forms.CharField(label=("Username"), max_length=30, help_text = ("Required"))
provider = forms.ChoiceField(widget=forms.Select(), choices=SOURCES_CHOICES, initial=SOURCES_CHOICES[1])
E = 'e'
A = 'a'
SOURCES_CHOICES = (
(A, 'A'),
(E, 'E'),
)
form = UserUsesSourceForm(initial={"username":request.user.username, 'provider':SOURCES_CHOICES[1]})return render_to_response('update_datasource.html', context_instance=RequestContext(request, params))
<form action="" method="post">
{% csrf_token %}
{% if form.non_field_errors %}
<p>
{% for error in form.non_field_errors %}
<div class="text-error">{{ error|escape }}</div>
{% endfor %}
</p>
{% endif %}
<div class="control-group">
<label class="control-label" for="id_provider">Data source</label>
<div class="controls">
{{form.provider}}
</div>
</div>
</form>
<select name="provider" id="id_provider">
<option value="A">A</option>
<option value="E">E</option>
</select>
option
value instead of tuple in initial
data:form = UserUsesSourceForm(
initial={'username':request.user.username,
'provider':SOURCES_CHOICES[1][0]})