Как сгенерировать таблицу из нескольких столбцов с вставкой формы флажка в первый столбец? - PullRequest
2 голосов
/ 02 марта 2012

Это мой файл 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>

1 Ответ

3 голосов
/ 02 марта 2012

django-tables2 упрощает преобразование данных в таблицы HTML.Это делает для таблиц HTML то, что django.forms делает для форм HTML ..

См. Это: http://django -tables2.readthedocs.org / en / latest / index.html

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...