扩展Django表温度

2024-03-29 14:25:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我想增加一个模板和一个单选按钮。请问我该怎么做?在

这是我的表格模板:

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

这是我的扩展表:

^{pr2}$

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

谢谢你的帮助


Tags: infieldsfortablebuttoncolitemtr
1条回答
网友
1楼 · 发布于 2024-03-29 14:25:32

一种方法是使用从模型类中获取BooleanField()-class对象的窗体。在

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单选按钮

^{pr2}$

相关问题 更多 >