我有以下功能,它应该做什么循环在一个名为groups的复选框中提交的值,然后将每个值提交给数据库。但是它似乎只向数据库添加一个值。有什么不对的吗?循环复选框并非所有值都添加到数据库中
基团= 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())
POST变量:
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>
forms.py
class BatchForm(forms.Form):
groups = forms.CharField(max_length=100)
请不要用'的项目范围(LEN(组))'。 '对于组中的项目'更清晰,那么'item'就是你需要的实际对象。 – 2013-03-21 10:18:31
你可以发布你的Django表单吗? – Vincent 2013-03-21 10:20:26
@Daniel for groups给我的列表索引必须是整数,而不是unicode – Prometheus 2013-03-21 10:48:07