【问题标题】:How to generate a multi-columns table with the checkbox form insert in the first column?如何使用在第一列中插入的复选框表单生成多列表?
【发布时间】:2012-03-20 17:01:04
【问题描述】:

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

我的模板的 sn-p 如下所示:

<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>

【问题讨论】:

    标签: python django forms checkbox


    【解决方案1】:

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

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

    【讨论】:

    • 我在试试,看看能不能做。
    • 所以 django-tables + django-filter 和你有你的管理员更改列表。这太棒了!
    • 教程说:“实例化表并传入数据”,数据是元组列表吗?
    猜你喜欢
    • 1970-01-01
    • 2012-01-08
    • 2014-07-18
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多