python - 如何生成在第一列插入复选框形式的多列表?

标签 python django forms checkbox

这是我的 forms.py 文件:

from django import forms

class MusicCheckboxForm(forms.Form):
    songs = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple())

我想使用 forms.py 文件生成多列表格,并将复选框插入到第一列,但我不想要硬代码 <input type="checkbox" ...>到模板。 我希望多列看起来像这样:

All song artist album genre date
[]  aaa  Smith  fdaf  rock 2001
[]  bbb  Davis  fdsaf blue 2002
[]  ccc  Doe    dfaj  sjaf 2000

我的模板片段如下所示:

<table class="list" summary="musics">
  <caption>
    music list
  </caption>
  <tr>
    <th>All</th>
    <th>song</th>
    <th>artist</th>
    <th>album</th>
    <th>genre</th>
    <th>date</th>
  </tr>
  {% for info in all_info %}
  <tr>
    <td>
      <!-- I want put checkbox here -->
    </td>

    {% if info|hasattr:"title" %}
      <td>{{ info.title }}</td>
    {% else %}
      <td></td>
    {% endif %}

    {% if info|hasattr:"artist" %}
      <td>{{ info.artist }}</td>
    {% else %}
      <td></td>
    {% endif %}

    {% if info|hasattr:"album" %}
      <td>{{ info.album }}</td>
    {% else %}
      <td></td>
    {% endif %}

    {% if info|hasattr:"genre" %}
      <td>{{ info.genre }}</td>
    {% else %}
      <td></td>
    {% endif %}

    {% if info|hasattr:"date" %}
      <td>{{ info.date }}</td>
    {% else %}
      <td></td>
    {% endif %}
  </tr>
  {% endfor %}
</table>

最佳答案

django-tables2 简化了将数据转换为 HTML 表格的过程。它对 HTML 表格的作用就像 django.forms 对 HTML 表单的作用一样......

看这个:http://django-tables2.readthedocs.org/en/latest/index.html

关于python - 如何生成在第一列插入复选框形式的多列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9529109/

相关文章:

python - 通过多行从 python 中提取

python - 在实时 Django 服务器上进行分析?

javascript - 通过 Ajax 上传文件附加数据,append() 不起作用

javascript - 外部 Javascript 脚本从未被调用

python - QFileDialog opens in new window while adding it to QHBoxLayout

python - 可以装饰类吗?

python - 检查列表是否包含特定的数字序列

javascript - 如何进行 django 就地编辑以及基于 AJAX 的 django 无尽分页

python - Django/Python : How do i transfer a class's attributes to another via a for loop?(表单->模型实例)

php - 无法以表单形式预先填充数据库中的数据