从表单中获取多个复选框值

时间:2019-03-18 06:41:03

标签: python django

我正在开发一个Webapp,其中显示了任务复选框。选择任务并提交后,将显示任务描述。但是,我编写的代码仅显示一个,而不显示多个。请帮我。 下面是我的代码

这是我的 urls.py

url(r'^view_task_description/$', views.view_task_description, name='view_task_description'),
url(r'^view_task_description/(?P<pk>[0-9]+)/$', views.get_task_description, name="get_task_description"),

这是我的 views.py

@login_required
def view_task_description(request):
    if request.method == 'POST':
        task_description = GetTaskDescription(data=request.POST, user=request.user)
        if task_description.is_valid():
            obj = GetTaskDescription.get_task_description(task_description)
            return redirect('get_task_description', pk=obj[0].pk)
    return render(request, 'todoapp/select_task_description.html', context={'view_tasks': GetTaskDescription(user=request.user)})


@login_required
def get_task_description(request, pk):
    obj = get_object_or_404(Task, pk=pk)
    return render(request, 'todoapp/task_desc.html', context={'description': obj.description})

这是我的 forms.py

class GetTaskDescription(forms.Form):

    get_tasks = forms.ModelMultipleChoiceField(
        queryset=Task.objects.none(),
        widget=forms.CheckboxSelectMultiple,
        required=True
    )

    def __init__(self, *args, **kwargs):
        self.user = kwargs.pop('user')
        super(GetTaskDescription, self).__init__(*args, **kwargs)
        self.fields['get_tasks'].queryset = self.user.task_set.all()

    def get_task_description(self):
        tasks = self.cleaned_data['get_tasks']
        return tasks

0 个答案:

没有答案