V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
iFlicker
V2EX  ›  问与答

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

  •  
  •   iFlicker · 2017-05-12 01:16:20 +08:00 · 2277 次点击
    这是一个创建于 2512 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 条回复    2017-05-12 01:46:05 +08:00
    iFlicker
        1
    iFlicker  
    OP
       2017-05-12 01:34:51 +08:00
    啊啊啊啊啊啊!!!
    原因是
    ```
    {'email', user.email,
    'sex', muser.sex,
    'birthday', muser.birthday,
    'website', muser.website,
    'qq', muser.qq,
    'introduction', muser.introduction
    }
    ```
    字典定义错了,冒号写成了逗号!!!
    啊我怎么这么蠢啊啊啊 ,翻来覆去看了半个小时没看出来。。。
    iFlicker
        2
    iFlicker  
    OP
       2017-05-12 01:46:05 +08:00
    状态不好睡觉睡觉,大家引以为戒,状态不好睡一觉起来或许就能发现问题(超低级错误 T.T)
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5430 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 08:42 · PVG 16:42 · LAX 01:42 · JFK 04:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.