Django 表单动态初始化报错, 求解~ - V2EX
iFlicker

Django 表单动态初始化报错, 求解~

  •  
  •   iFlicker May 12, 2017 2982 views
    This topic created in 3287 days ago, the information mentioned may be changed or developed.

    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 值),因为是要从数据库拿数据。

    2 replies    2017-05-12 01:46:05 +08:00
    iFlicker
        1
    iFlicker  
    OP
       May 12, 2017
    啊啊啊啊啊啊!!!
    原因是
    ```
    {'email', user.email,
    'sex', muser.sex,
    'birthday', muser.birthday,
    'website', muser.website,
    'qq', muser.qq,
    'introduction', muser.introduction
    }
    ```
    字典定义错了,冒号写成了逗号!!!
    啊我怎么这么蠢啊啊啊 ,翻来覆去看了半个小时没看出来。。。
    iFlicker
        2
    iFlicker  
    OP
       May 12, 2017
    状态不好睡觉睡觉,大家引以为戒,状态不好睡一觉起来或许就能发现问题(超低级错误 T.T)
    About     Help     Advertise     Blog     API     FAQ     Solana     3516 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 43ms UTC 12:05 PVG 20:05 LAX 05:05 JFK 08:05
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86