Python 如何从请求(django)中的复选框中检索所有数据?

Python 如何从请求(django)中的复选框中检索所有数据?,python,django,select,checkbox,Python,Django,Select,Checkbox,我有一个表单,可以选择多个复选框,但是当我在post请求中尝试从字典中检索密钥时。它只提供数组的第一个值(可能不是数组)。我想检索所有的数据点 模型 class CreateJournalistForm(forms.Form): vertical_choices = (('General', 'General'), ('News', 'News'), ('Life', 'Life'), ('Money', 'Money'), ('Sports', 'Sports'), ('Entert

我有一个表单,可以选择多个复选框,但是当我在post请求中尝试从字典中检索密钥时。它只提供数组的第一个值(可能不是数组)。我想检索所有的数据点

模型

class CreateJournalistForm(forms.Form):

    vertical_choices = (('General', 'General'), ('News', 'News'), ('Life', 'Life'), ('Money', 'Money'), ('Sports', 'Sports'), ('Entertainment', 'Entertainment'), ('Dating', 'Dating'), ('Music', 'Music'), ('Humor', 'Humor'))

    name = forms.CharField(max_length=40, required=False)
    verticals = forms.MultipleChoiceField(required=True, widget=forms.CheckboxSelectMultiple, choices=vertical_choices)
    location = forms.CharField(max_length=40)
看法

In-pry调试器请求岗将提供:

<QueryDict: {u'sample': [u'sdfsdf'], u'first_name': [u'sdfsd'], u'last_name': [u'sdfsf'], u'name': [u''], u'dob': [u'1/22/13'], u'twitter': [u'sdfsdf'], u'ideas': [u'sdfsdf'], u'contribution': [u'sdf'], u'verticals': [u'General', u'News', u'Life', u'Money', u'Sports'], u'google': [u'sdfsdf'], u'location': [u'sdfsdf']}

我不知道您为什么要直接访问请求,因为您有一个表单,它负责将参数转换为对象


但是,您可以使用
request.POST.getlist('verticals')
获取一个键的所有值。

这很有效!如何使用表单获取数据?您应该使用
form.data
在表单验证后访问数据。这些都在表单文档中进行了解释,向用户显示错误的正确模式也是如此。
<QueryDict: {u'sample': [u'sdfsdf'], u'first_name': [u'sdfsd'], u'last_name': [u'sdfsf'], u'name': [u''], u'dob': [u'1/22/13'], u'twitter': [u'sdfsdf'], u'ideas': [u'sdfsdf'], u'contribution': [u'sdf'], u'verticals': [u'General', u'News', u'Life', u'Money', u'Sports'], u'google': [u'sdfsdf'], u'location': [u'sdfsdf']}