在视图中迭代逗号分隔列表

时间:2013-03-20 16:20:40

标签: django

如何循环查看视图中的复选框值。

这是我到目前为止所尝试的:

if request.method == 'POST':
        form = SomeForm(request.POST)
        # Check we have valid data before saving trying to save.
        if form.is_valid():
            data = form.cleaned_data
            groups = data['thischeckbox']
            for item in groups:
                print(item)

但这会产生 KeyError

1 个答案:

答案 0 :(得分:1)

if request.method == 'POST':
    form = SomeForm(request.POST)
    # Check we have valid data before saving trying to save.
    if form.is_valid():
        data = form.cleaned_data
        groups = data['thischeckbox'].split(",")
        for item in range(len(groups))
            print(groups[item])