扩展Django表模板

时间:2015-11-04 13:43:15

标签: python django django-templates

我有一个表模板,我想扩展它并添加一个单选按钮。我该怎么办呢?

这是我的表格模板:

<table class="table table-striped">
  <thead>
    <tr>
      {% for heading in headings %}
      <th class="col-head-{{ fields|get_item:forloop.counter0 }}">{{ heading }}</th>
      {% endfor %}
      <th>&nbsp;</th> <!--  edit  -->
      <th>&nbsp;</th> <!-- delete -->
    </tr>
  </thead>
  <tbody>
    {% for item in objects %}
    <tr class="row-clickable">
      {% for field in fields %}
      <td class="col-item-{{ field }}">
        {% block cell %}
          {{ item|get_item:field }}
        {% endblock %}
      </td>
      {% endfor %}
      <td class="col-icon col-icon-edit"><button type="button"     class="btn btn-sm btn-default"><span class="glyphicon glyphicon-pencil">    </span></button></td>
      <td class="col-icon col-icon-remove"><button type="button"     class="btn btn-sm btn-default"><span class="glyphicon glyphicon-trash">    </span></button></td>
    </tr>
    {% empty %}
    <tr>
      <td colspan="{{ fields|length }}">Nothing yet to display.</td>
      <td colspan="2">&nbsp;</td>
    </tr>
    {% endfor %}
  </tbody>
</table>

这是我的扩展表:

{% extends "frontend/table.html" %}
{% load frontend_extras %}
{% block cell %}
  {% if field == "name" %}
    {% if item.thumbnail %}
      <a class="content-preview" href="{{ item.path }}" data-    mimeType="{{ item.content_format }}" data-name="{{ item.name }}">
        <img src="{{ item.thumbnail }}" class="img-thumbnail content-    thumbnail">
      </a>
      <a class="content-name content-with-thumbnail content-preview"   href="{{ item.path }}" data-mimeType="{{ item.content_format }}" data-  name="{{ item.name }}">
        {{ item.name }}
      </a>
    {% else %}
      <a class="content-name content-no-thumbnail content-preview" href="{{ item.path }}" data-mimeType="{{ item.content_format }}" data-name="{{ item.name }}">
        {{ item.name }}
      </a>
    {% endif %}
  {% elif field == "content_size" %}
    {{ item|get_item:field|filesizeformat }}
  {% elif field == "content_duration" %}
    {% if item|get_item:field %}
      {{ item|get_item:field|duration }}
    {% else %}
      &nbsp;
    {% endif %}
  {% else %}
    {{ item|get_item:field }}
  {% endif %}
{% endblock %}

如何在扩展表中添加单选按钮。

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

一种方法是使用从模型类中获取BooleanField() - 类对象的表单。

in models.py

class MyClass(models.Model):

    radiobutton = models.BooleanField()

in forms.py
from myapp import 
class MyForm(forms.ModelForm):
    class Meta:
       model = MyClass
       fields = ('radiobutton',)

in template
 # where you want the button to appear 
 <form action="" method="post" >{% csrf_token %}
     {{form.as_p }}

  </form>

# you will also have to validate the form in your viewsfunction that returns the 
# the template

或者你可以使用Ajax radiobutton

<label class="radio-inline">
        <input type="radio" name="name" id="" value="1">
      </label>