2015-11-04 147 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