这是 NOT 只是另一个阅读Django复选框。我在这里阅读了7篇关于Checkbox的不同帖子以及Django文档中的其他4个页面。我的情况有点不同。我无法读取是否选中了特定的复选框。即使只选中了一个复选框,我也只为两个复选框获得一个值“True”。如果没有检查,它按预期工作。我真的需要使用MultipleChoiceField吗?
当前输出:
- John Doe Location A True ===> Checked
- James Smith Location A True ===> Unchecked
理想情况下,我想要一个包含
的字典列表data [0] = {'John', 'Doe', 1}
data [1] = {'John', 'Smith', 0}
...
其中'1'是覆盖的标志,'0'是忽略。
背景
forms.py
from django import forms
from django.forms.formsets import BaseFormSet
class NameForm (forms.Form):
first_name = forms.CharField (max_length = 20, required = False)
last_name = forms.CharField (max_length = 20, required = False)
class BaseNameFormSet (BaseFormSet):
...
class CheckBox (forms.Form):
overwrite = forms.BooleanField (required = False)
views.py
def addname (request):
....
if request.method == 'POST':
....
if formset.is_valid ():
location = request.POST ['site']
data = formset.cleaned_data
# Store data into session to be used in another method
request.session ['location'] = location
request.session ['data'] = data
def process (request):
location = request.session ['location']
data = request.session ['data']
if request.method == 'POST':
form = CheckBox (request.POST)
if form.is_valid ():
overwrite = form.cleaned_data.get ('overwrite')
# for duplicate in checkboxes:
# overwrite = duplicate.get ('overwrite')
print (overwrite)
context = {'data': data, 'location': location, 'overwrite': overwrite}
return render (request, 'nodeform/success.html', context)
return HttpResponse ('No Overwrite Data.')
response.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static 'nameform/style.css' %}" >
<title>Submitted Entries</title>
</head>
<body>
<h1>Submitted Entries:</h1>
<h4>Location: {{ location }}</h4>
<form action="process" method="POST">{% csrf_token %}
<div id="tablefont">
<table id="table01">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th class="center">Overwrite</th>
</tr>
{% for info in data %}
<tr>
<td>{{ info.first_name }}</td>
<td>{{ info.last_name }}</td>
<td class="center"><input type="checkbox" name='overwrite-{{ forloop.counter0 }}'></td> ===> Testing Checkbox
<!--
{% if info.overwrite %}
<td class="center"><input type="checkbox" name='overwrite-{{ forloop.counter0 }}'></td>
{% else %}
<td class="center"></td>
{% endif %}
-->
</tr>
{% endfor %}
</table>
</div>
<br>
{% if errors %}
<p class="errorlh">Error:
<ul>
{% for error in errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
</p>
{% endif %}
<br>
<p><input type="submit" value="Confirm">
<a href="{% url 'addname' %}">
<button type="button">Cancel</button></a></p>
</form>
</body>
</html>
success.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Successfully Added</title>
</head>
<body>
<h1>Information captured:</h1>
<ul>
{% for info in data %}
<li>{{ info.first_name }} {{ info.last_name }} {{ location }} {{ overwrite }}</li>
{% endfor %}
</ul>
<a href="{% url 'addname' %}">Add more names</a>
</body>
</html>
答案 0 :(得分:0)
您的所有复选框输入都具有相同的属性name=overwrite
,因此,当您选中一个时,它将是POST overwrite=true
中提交的唯一一个。
您应该为每个输入指定一个唯一的名称。我建议你使用{{ forloop.counter0 }}
,它将提供循环的当前迭代,从0开始。基本上它是“data”中当前“info”的索引。
{% for info in data %}
<tr>
<td>{{ info.first_name }}</td>
<td>{{ info.last_name }}</td>
<td class="center"><input type="checkbox" name='overwrite-{{ forloop.counter0 }}'></td> ===> Testing Checkbox
<!--
{% if info.overwrite %}
<td class="center"><input type="checkbox" name='overwrite' value="1"></td>
{% else %}
<td class="center"></td>
{% endif %}
-->
</tr>
{% endfor %}
这样,您可以将POST中的表单字段与后端中的“info”进行匹配。