我正在使用django:
我正在尝试将views.py
中的元组列表传递给下拉框形式,但出现此属性错误forms.py
import logging
from django import forms
log = logging.getLogger(__name__)
class TestForm(forms.Form):
def __init__(self, *args, **kwargs):
testlist = kwargs.pop('testlist',None)
log.info(regionlist)
self.fields['testlist'] = forms.ChoiceField(choices=testlist)
super(TestForm, self).__init__(*args, **kwargs)
views.py
form = forms.RegionForm(regionlist=data)
我是否使用正确的方法在
views.py
和forms.py
之间传递变量? 最佳答案
您需要先调用super
,以便父类(super class)设置fields
属性。
def __init__(self, *args, **kwargs):
testlist = kwargs.pop('testlist', None)
log.info(regionlist)
super(TestForm, self).__init__(*args, **kwargs)
self.fields['testlist'] = forms.ChoiceField(choices=testlist)