我有以下功能,它应该做什么循环在一个名为组的复选框中提交的值,然后将每个值提交到数据库。但是,它似乎只向数据库添加了一个值。有什么不对的吗?
组 = 1,3,3,4
功能更新
def add_batch(request):
# If we had a POST then get the request post values.
if request.method == 'POST':
form = BatchForm(request.POST)
# Check we have valid data before saving trying to save.
if form.is_valid():
# Clean all data and add to var data.
data = form.cleaned_data
groups = data['groups'].split(",")
for item in groups:
batch = Batch(content=data['message'],
group=Group.objects.get(pk=item),
user=request.user
)
batch.save()
return redirect(batch.get_send_conformation_page())
发布变量:
groups 1, 3, 4
形式:
<form action="{% url 'add_batch' %}" method="post" class="form-horizontal" enctype="multipart/form-data" >
{% for item in groups %}
<label class="groups">
<input type="checkbox" name="groups" value="{{ item.id }}" /> {{item.name}}<br />
</label>
{% endfor %}
</form>
表格.py
class BatchForm(forms.Form):
groups = forms.CharField(max_length=100)