
views 里相关函数 部分如下:
if request.method == 'POST': ... else: form = SettingForm(data={'email', user.email, 'sex', muser.sex, 'birthday', muser.birthday, 'website', muser.website, 'qq', muser.qq, 'introduction', muser.introduction }) return render(request, 'setting.html', {'form': form, 'user': user}) 不管 SettingForm 中参数是 initial 还是 data,或者是其内容字典内为数据库查询结果还是固定字符串
都会报错 'set' object has no attribute 'get'
定位在模板中的 {{ form.email }}
Traceback 在
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/forms/boundfield.py in initial widget = self.field.widget id_ = widget.attrs.get('id') or self.auto_id return widget.id_for_label(id_) @property def initial(self): data = self.form.initial.get(self.name, self.field.initial) if callable(data): if self._initial_value is not UNSET: data = self._initial_value else: data = data() # If this is an auto-generated default date, nix the 中的 data = self.form.initial.get(self.name, self.field.initial) 这行
或是在
/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/django/forms/widgets.py in value_from_datadict return attrs def value_from_datadict(self, data, files, name): """ Given a dictionary of data and this widget's name, returns the value of this widget. Returns None if it's not provided. """ return data.get(name) def value_omitted_from_data(self, data, files, name): return name not in data def id_for_label(self, id_): """ 中的 return data.get(name) 这行
不知道是哪儿的问题,翻了文档也是一脸懵逼。。。
如果这两个参数都不对的话,请问有什么办法能动态的在 Form 对象创建的时候初始化数据(<input>标签的 value 值),因为是要从数据库拿数据。
1 iFlicker OP 啊啊啊啊啊啊!!! 原因是 ``` {'email', user.email, 'sex', muser.sex, 'birthday', muser.birthday, 'website', muser.website, 'qq', muser.qq, 'introduction', muser.introduction } ``` 字典定义错了,冒号写成了逗号!!! 啊我怎么这么蠢啊啊啊 ,翻来覆去看了半个小时没看出来。。。 |
2 iFlicker OP 状态不好睡觉睡觉,大家引以为戒,状态不好睡一觉起来或许就能发现问题(超低级错误 T.T) |