从窗体中的复选框获取标签

2024-06-23 03:14:51 发布

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

我有一个表单,它有一些字段:一个文本区域,一个文本框和随机数的复选框,这些复选框根据产品的不同而变化。我想知道如何获得选中复选框的标签。你知道吗

这是我的表格:

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

Tags: brforminputtypelabelh3placeholderclass
2条回答
  • 假设您将表单作为POST发送,则 选中的复选框位于request.POST.getlist('tag')。你知道吗

    请注意,邮件中只发送复选框,但列表 包含所有选中框的值元素。

例如:

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

检查值将包含[1,4](已检查的值)

在型号.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 %}

相关问题 更多 >

    热门问题