这是我的 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/