首页 > 解决方案 > 使用 Django 发送带有复选框值更改的 POST

问题描述

我有一张表格,表格中有一个表格,其中包含用户可以更改的字段。其中三个字段使用复选框。在关注这篇文章之后,我能够反映数据库中当前的内容,但我无法为下一个难题找到其他内容。

我遇到的问题是,如果一个复选框设置为 true(checked),然后我取消选择它,则 POST 数据不包含任何表明它不再被选中的内容。它没有给出“off”、“0”或 false 的值。我如何确保在未选中这些框时我仍然在发布数据上获得这些值(“off”、0“或“false”)?

下面是我的代码:

<form id=result_form action= "{% url 'example:save_notes' %}" method="post">
{% csrf_token %}
    <table id="results_table" class="formdata" summary="Example Search Results"
    data-order='[[ 1, "des" ]]' data-page-length='25'>
        <thead>
            <tr>
                <th style="width:5%" scope="col">Search Method</th> 
                <th style="width:20%" scope="col">Search Date</th>
                <th style="width:5%" scope="col">City</th>
                <th style="width:10%" scope="col">Example Name</th>
                <th style="width:10%" scope="col">Article Title</th>
                <th style="width:10%" scope="col">Article Link</th>
                <th style="width:5%" scope="col">Reviewed </th>
                <th style="width:5%" scope="col">J/J Related</th>
                <th style="width:5%" scope="col">N Related</th>
                <th scope="col">General Notes</th>
                <th scope="col">Findings</th>
                <th scope="col">Edit</th>

            </tr>
        </thead>
    <tbody>
    {% for result in results %}
        <tr>
            <td>{{result.notes.method}}</td>
            <td>{{result.retrieved_on}}</td>
            <td>{% for e in result.search_result_t.all%}{{e.search_term_id.example_abbrev}}&nbsp{% endfor %}</td>
            <td>{% for e in result.search_result_t.all%}{{e.search_term_id.example_ID.example_name}}{% endfor %}</td>
            <td>{{result.title}}</td>
            <td width="5%"><a href={{result.link}} target="_blank">{{result.link}}</a></td>
            <form action="{% url 'example:form_edit' %}" method="POST">
            <td><input type="checkbox" name="reviewed" {% if result.notes.reviewed %}checked{% endif %}></td>
            <td><input type="checkbox" name="jj" {%if result.notes.j_j %}checked{% endif %}></td>
            <td><input type="checkbox" name="n" {%if result.notes.n_related %}checked{% endif %}></td>
            <td><textarea cols="40" rows="15" name="general_notes" value = "{{result.notes.general_notes}}">{{result.notes.general_notes | default_if_none:""}}</textarea>
            <td><textarea cols="40" rows="15" name="significant_findings" value = "{{result.notes.significant_findings}}">{{result.notes.significant_findings | default_if_none:""}}</textarea>
            <td><button>Save</button></td>    
            <input type="hidden" name="result.id" value={{result.id}}>
            </form>
        </tr>
        {% endfor %}

标签: htmldjangodjango-templates

解决方案


未选中的框不是请求的一部分 - 请参见此处。但是您为什么需要此信息,因为您应该知道您发送了哪些复选框。


推荐阅读