Django提取字段表单html

时间:2018-04-30 01:54:29

标签: django forms

我有一个html页面,它返回表单中的数据。表单是带有"name=states1"的多选复选框字段。当我在forms.py中运行print(vars(self))时,我看到数据存在['MD', 'WV', 'WY'](见下文)但是,当我尝试用print(self.data['states1'])提取数据时,我只是得到'WY '和print(len(self.data['states1']))返回2.

{'instance': <AA: AAobject>, '_validate_unique': False, 'is_bound': True, 'data': <QueryDict: {'csrfmiddlewaretoken': ['AAAAA']: ['1'], 'states1': ['MD', 'WV', 'WY'], 'states': [''], 'price': ['2.00'], 'user': ['1', '1']}>, ...

然而,当我跑

import json
print(json.dumps(self.data, indent=2))

我得到以下输出

    {
  "csrfmiddlewaretoken": "AAAAA",
  "roles": "1",
  "states1": "WY",
  "states": "",
  "price": "2.00",
  "user": "1"
}

数据刚刚消失。为什么我的数据被限制在最后一个字段,当我在查看变量时能够清楚地看到它?

forms.py

class UserProfileChangeForm(forms.ModelForm):
states = forms.CharField(widget=USStateSelect(), initial='TX', required=False)

class Meta:
    model = SkilledLaborer
    fields = ['user','roles','travel_flag','price','states']


def clean_states(self):
    print('states')
    states = self.cleaned_data['states']
    print(type(vars(self)))
    print(vars(self).keys())
    print(vars(self))
    print(self.data.keys())
    print(self.data['states1'])
    print(len(self.data['states1']))

    import json
    print(json.dumps(self.data, indent=2))

    try:
        self.data['states1']
    except:
        pass

    return states

0 个答案:

没有答案