无法从django中获取多个选择的选项

时间:2016-09-03 16:06:06

标签: python django couchdb

我在获取多个选择表单的选项时遇到了问题。我正试图从couchdb中做出选择。它已成功打印到控制台中:
[[u'c6570a56173b637d66ba2a2e390271fe',u'Rambler'],[u'c6570a56173b637d66ba2a2e3902ad1f',u'BBC']]
,但它不会出现在模板中。
这是我的 forms.py

sel = []


# FiltersForm is print out title, two select elements and an one required textinput's field
class FiltersForm(forms.Form):
    title = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'A title'}), label='Title')
    item = forms.ChoiceField(widget=forms.Select(attrs={'class': 'selectpicker'}), required=False, label='If',
                             choices=items)
    action = forms.ChoiceField(widget=forms.Select(attrs={'class': 'selectpicker'}), required=False, label='is',
                               choices=actions)
    word = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'a word'}))
    link = forms.URLField(max_length=255, widget=forms.URLInput(attrs={'value': 'http://'}))
    source = forms.MultipleChoiceField(widget=forms.SelectMultiple(attrs={'class': 'selectpicker'}), choices=sel)

    def __init__(self, *args, **kwargs):
        request = kwargs.pop('request', None)
        response = request.db.view('subscriptions/source', key=str(request.user)).rows
        for item in response:
            sel.append([item.id, item.value['title']])
        print sel
        super(FiltersForm, self).__init__(*args, **kwargs)

views.py中的表单实例

# Retrieving a FiltersForm
form = FiltersForm(request.POST or None, request=request)

我的表格有什么问题?

1 个答案:

答案 0 :(得分:1)

您已在sel方法中定义了一个名为__init__的局部变量,但这与您在全球范围内具有相同名称的变量没有任何关系用于填充表单。您实际上必须使用该方法中的新值替换选项:

self.fields['source'].choices = sel