无法使用Select as窗口小部件迭代ChoiceField [英] Can not iterate a ChoiceField with Select as widget

查看:118
本文介绍了无法使用Select as窗口小部件迭代ChoiceField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,迭代一个 ChoiceField ,并从值和标签构建我自己的HTML。当指定widget参数为选择时,该字段不再是可迭代的。



但是,如果将其指定为 RadioSelect



表单:

  MyFormCreate(Form):
QUOTES =(
(34,Patrick,等等,我是PATRICK!),
(21,我喜欢馅饼),
(76,不,这是Patrick!),

patrick = ChoiceField(choices = QUOTES,widget = Select)
pre>

模板:

 < select name = {{form.patrick.name}}> 
{%for form.patrick%}
< option value ={{option.choice_value}}> {{option.choice_label}}< / option>
{%endfor%}
< / select>

我做错了什么?



(Python 2.7.3和Django 1.4.5)

解决方案

这将是您要找的?

 < select name ={{form.patrick.name}}> 
{%的值,form.patrick.field.choices%中的文本
< option value ={{value}}> {{text}}< / option>
{%endfor%}
< / select>

此外,空格是你的朋友。 :)


I have issues with iterating over a ChoiceField and building my own HTML from the values and labels. When specifying "widget" parameter as Select, the field is not longer iterable.

However, it works fine if I specify it as RadioSelect.

The form:

class MyFormCreate( Form ) :
    QUOTES = (
            (34, "Hi, Patrick. Wait, I'M PATRICK!"),
            (21, "I like pie."), 
            (76, "No, this is Patrick!"),
    )
    patrick = ChoiceField(choices = QUOTES, widget = Select)

And the template:

<select name="{{form.patrick.name}}">
    {% for option in form.patrick %}
    <option value="{{option.choice_value}}">{{option.choice_label}}</option>
    {% endfor %}
</select>

What am I doing wrong?

(Python 2.7.3 and Django 1.4.5)

解决方案

Would this be what you're looking for?

<select name="{{ form.patrick.name }}">
    {% for value, text in form.patrick.field.choices %}
        <option value="{{ value }}">{{ text }}</option>
    {% endfor %}
</select>

Also, white space is your friend. :)

这篇关于无法使用Select as窗口小部件迭代ChoiceField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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