2015-11-04 146 views
0

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

这是我的表模板:

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

我如何添加一个单选按钮来扩展表。

感谢您的帮助

+0

的常用方法是使用你在你的模板调用一个形式。您可以在模型中使用BooleanField()创建单选按钮,并将其通过表单传递给模板。 – user1749431

回答

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

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

看到这个SO邮政阿贾克斯radiobutton http://stackoverflow.com/questions/30147012/set-radio-button-checked-with-ajax-response – user1749431

+0

虐待它尝试它。谢谢 – user3232446