html - 使用 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}} {% 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 %}
解决方案
未选中的框不是请求的一部分 - 请参见此处。但是您为什么需要此信息,因为您应该知道您发送了哪些复选框。
推荐阅读
- django-rest-framework - Django REST Framework:在保留标准视图集的同时排除特定路由
- flutter - 运算符“??”是什么意思 在 Dart/Flutter 中?
- c++ - C++:将函数存储在字节数组中,然后通过函数指针执行函数
- performance - 从 HCL Connections 对 Kudos Boards WebSphere 中的慢速 ajax 请求进行故障排除
- c# - 带有参数的 Azure 函数上的两个计时器触发器
- r - 闪亮的反应条形图与聚集
- javascript - return !!this.variable 是什么意思?
- php - 教义结果给无限循环
- ionic4 - inappbrowser IONIC 4 中的视频聊天
- python - 具有字符条件的 Pandas sum()