忽略django表单选择字段的初始值 [英] initial value for django form choice field ignored

查看:178
本文介绍了忽略django表单选择字段的初始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个表单:

  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>

..而我希望它有一个默认或活动选项。
请注意,用户名字段已正确初始化。
如何进一步调查以确定问题在哪里?

解决方案

您需要传递 form = UserUsesSourceForm(
initial = {'username':request.user.username,
'provider':SOURCES_CHOICES [1] [0]})


I have this form:

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])

The available choices are:

E = 'e'
A = 'a'
SOURCES_CHOICES = (
                  (A, 'A'),
                  (E, 'E'),
                  )

The view:

form = UserUsesSourceForm(initial={"username":request.user.username, 'provider':SOURCES_CHOICES[1]})return render_to_response('update_datasource.html', context_instance=RequestContext(request, params))

And the template:

<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>

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:

<select name="provider" id="id_provider">
<option value="A">A</option>
<option value="E">E</option>
</select>

..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 option value instead of tuple in initial data:

form = UserUsesSourceForm(
    initial={'username':request.user.username,
             'provider':SOURCES_CHOICES[1][0]})

这篇关于忽略django表单选择字段的初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆