2014-02-10 59 views
0

我有一个表格有一些字段:一个文本框,一个文本框和随机数量的复选框,这取决于产品。我想知道如何获取选中的复选框的标签。从表格中的复选框获取标签

这是我的形式:

<form class="form-inline"> 
<strong><h3>Revise este produto</h3></strong><br> 

{% for tag in tags %} 
<label class="checkbox"> 
<input type="checkbox" value=""> #Ótimo 
</label> 
{% endfor %} 

<br/> 
<p>&nbsp;</p> 
<label>Envie outras hashtags</label> <br/> 
<input type="text" class="span3" placeholder="exemplo1, exemplo2"> 
<br /> 
<p>&nbsp;</p> 
<label>Deixe sua opinião (opcional)</label> <br/> 
<textarea name="Text1" cols="80" class="span3" rows="5" placeholder="Digite sua opinião aqui"></textarea> 
<br/> 
<p>&nbsp;</p> 
<button class="btn btn-primary" type="submit"><h4>Pronto!</h4></button> 
</form> 

回答

1

在models.py中:

class Tag: 
    published = BooleanField() 
    (...) 

模板中:

{% for tag in tags %} 
<label class="checkbox"> 
<input type="checkbox" name="tag[]" value="" {% if tag.published %}checked{% endif %}> #Ótimo 
</label> 
{% endfor %} 
1
  • 假设你要发送的形式作为POST的 选中复选框的值是request.POST.getlist('tag')

    请注意,POST中只发送过选中的框,但列表 包含所有检查框的值元素。

例如:

<input type="checkbox" name="tag[]" value="1" /> 
<input type="checkbox" name="tag[]" value="2" /> 
<input type="checkbox" name="tag[]" value="3" /> 
<input type="checkbox" name="tag[]" value="4" /> 

说,如果1,4-进行了检查,

check_values = request.POST.getlist('checks') 

check_values将包含[1,4](那些已检查值)

相关问题