BoundField'对象不可迭代 - Django

时间:2013-05-13 15:23:06

标签: django django-forms django-templates

forms.py

Date_Format = (
    ('0', ' dd / mm / yyyy'),
    ('1', 'mm / dd / yyyy'),
)    
Time_Format = (
    ('0', ' 12 hour AM / PM '),
    ('1', ' 24 hour '),
)
class SettingsForm(forms.ModelForm):
    date_format = forms.ChoiceField(widget=forms.RadioSelect(), choices=Date_Format)
    time_format = forms.ChoiceField(widget=forms.RadioSelect(), choices=Time_Format)

template.py

{% for radio in SettingsForm.date_format %}
 {{ radio.choice_label }}
 <div class="select">{{ radio.tag }}</div>
 {% endfor %}

回溯

Template error:
In template /root/Projects/ir/templates/incident/date_format.html, error at line 107
   Caught TypeError while rendering: 'BoundField' object is not iterable
   97 :                 <tr>
   98 :                     <td style="width:200px;">
   99 : 
   100 :                         {{ SettingsForm.date_format }}
   101 :                         {% for radio in form.date_format %}
   102 :                         
   103 :                         <div >{{ radio }}</div>
   104 :                         {% endfor %}
   105 :                     </td>
   106 :                     <td>eg 21 / 01 / 2014 <br /> 
   107 :        {% for date in SettingsForm.time_format %} 
   108 :         {{ date.label_tag }}
   109 :         {{ date }}
   110 :       {% endfor %} 
   113 :                 </tr>
Traceback:
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/root/Projects/ir/incident/views.py" in date_format
  796.                  context_instance=RequestContext(request))
File "/usr/lib/python2.7/site-packages/django/shortcuts/__init__.py" in render_to_response
  20.     return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/usr/lib/python2.7/site-packages/django/template/loader.py" in render_to_string
  188.         return t.render(context_instance)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in render
  123.             return self._render(context)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in _render
  117.         return self.nodelist.render(context)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/usr/lib/python2.7/site-packages/django/template/debug.py" in render_node
  73.             result = node.render(context)
File "/usr/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  127.         return compiled_parent._render(context)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in _render
  117.         return self.nodelist.render(context)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/usr/lib/python2.7/site-packages/django/template/debug.py" in render_node
  73.             result = node.render(context)
File "/usr/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  127.         return compiled_parent._render(context)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in _render
  117.         return self.nodelist.render(context)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/usr/lib/python2.7/site-packages/django/template/debug.py" in render_node
  73.             result = node.render(context)
File "/usr/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  127.         return compiled_parent._render(context)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in _render
  117.         return self.nodelist.render(context)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/usr/lib/python2.7/site-packages/django/template/debug.py" in render_node
  73.             result = node.render(context)
File "/usr/lib/python2.7/site-packages/django/template/loader_tags.py" in render
  64.             result = block.nodelist.render(context)
File "/usr/lib/python2.7/site-packages/django/template/base.py" in render
  744.                 bits.append(self.render_node(node, context))
File "/usr/lib/python2.7/site-packages/django/template/debug.py" in render_node
  73.             result = node.render(context)
File "/usr/lib/python2.7/site-packages/django/template/defaulttags.py" in render
  190.             values = list(values)

Exception Type: TemplateSyntaxError at /member/date-format/
Exception Value: Caught TypeError while rendering: 'BoundField' object is not iterable

我正在使用django 1.3.7。

由于单选按钮在无序列表中呈现,我使用单选按钮自定义。

我在定制单选按钮时收到此错误。

更新

通常如果我渲染一个无线电选择按钮,将在页面中的列表中呈现。如果我希望它显示没有该列表格式,即没有项目符号点。如何使用CSS或任何其他技术

由于

1 个答案:

答案 0 :(得分:0)

更改行

{% for radio in SettingsForm.date_format %}

{% for radio in SettingsForm.date_format.field.choices %}