【问题标题】:Django radio form showing ['on'] for the POST values显示 POST 值的 ['on'] 的 Django 单选表单
【发布时间】:2018-11-24 07:57:51
【问题描述】:

当我使用 Django 表单时,我需要在提交时获取选定的单选按钮 ID。但是,当我记录发布数据的值时,我发现 request.POST 选择的收音机为 'sample': ['on']。

模板/detail.html

<form action="{% url 'polls:vote' question.id %}" method="post">
    {% csrf_token %}
    <input type="radio" name="sample" id="r1">
    <label for="r1">Radio 1</label>
    <input type="radio" name="sample" id="r2">
    <label for="r2">Radio 2</label>
    <br>
    <input type="submit" value="Vote">
</form>

views.py

print (request.POST)

打印结果

<QueryDict: {'csrfmiddlewaretoken': ['exomyfdxW1uPeZtZA46SgWG9UVxX8iY6SGypagSDmkYdeFcifCOXRiXQv2TIgp2A'], 'sample': ['on']}>

为什么会这样是我的代码有问题。

【问题讨论】:

  • 您期待看到什么?您没有设置单选按钮的value
  • 对不起,我认为它的 id 会被传递
  • 试试&lt;input type="radio" name="sample" id="r1" value="r1"&gt;

标签: django django-forms


【解决方案1】:

你需要给输入一个值。

<form action="{{ url_for('show_10') }}" value="1">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-27
    • 2013-02-14
    • 2013-10-15
    • 2016-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多