用django形成如何组织多列中的字段?

时间:2012-09-27 13:27:06

标签: django django-forms django-templates

class MyForm(forms.Form):
  row_1 = forms.ChoiceField(widget=forms.RadioSelect, choices=CHOICES)
  row_2_col_1 = forms.ChoiceField(widget=forms.RadioSelect, choices=CHOICES)
  row_2_col_2 = forms.ChoiceField(widget=forms.RadioSelect, choices=CHOICES)

  def render_form(request):
    form = MyForm()
    # render form

我的模板应该是什么,以便最终的html类似于

<table>
<tr> row_1 field.. </tr>
<tr>
  <td> row_2_col_1 field.. </td>
  <td> row_2_col_2 field.. </td>
</tr>

1 个答案:

答案 0 :(得分:1)

如果您已将表格传递给请求中的表单,则只需使用Django模板标记:

<table>
<tr>{{ table.row_1 }}</tr>
<tr>
  <td>{{ table.row_2_col_1 }}</td>
  <td>{{ table.row_2_col_2 }}</td>
</tr>