4

我是 python 和烧瓶框架的新手。

我的问题是我必须制作一个编辑页面。所以我需要将所有对象的存在信息传递给一个表单。细节:

我的对象有这种结构:

class TVChannel(Document):
    __collection__  = 'tvchannels'
    use_dot_notation = True

    structure = {
        '_id'           : basestring,
        'name'          : unicode,
        'streams'       : {
            'city1': { 
                'adapt' : basestring,
                'hds'   : basestring,
                'hls'   : basestring,
                'rtmp'  : basestring,
            },       
            'city2':  {
                'adapt' : basestring,
                'hds'   : basestring,
                'hls'   : basestring,
                'rtmp'  : basestring,
            }
        }
    }

在视图中:

channel_obj = db.TVChannel().get_id(channel_id) #load a channel's datas into an object from db
channel     = ChannelForm(request.form, obj=channel_obj) #load channel form

return render_template('channel/new.html',form=channel, channel_id=channel_id)

他们理解并像往常一样传递通道对象的“名称”。但是“流”,他们没有识别并将其传递给这样的东西:

class ChannelForm(Form):    
    _id         = HiddenField()
    name        = TextField(_('channel name'))    
    streams = {
        'city1': { 
            'adapt' : TextField(_('stream adapt link')),
            'hds'   : TextField(_('stream hds link')),
            'hls'   : TextField(_('stream hls link')),
            'rtmp'  : TextField(_('stream rtmp link')),
        },       
        'city2':  {
            'adapt' : TextField(_('stream adapt link')),
            'hds'   : TextField(_('stream hds link')),
            'hls'   : TextField(_('stream hls link')),
            'rtmp'  : TextField(_('stream rtmp link')),
        }
    }

    submit      = SubmitField(_('Save'))

我该怎么办?或者无论如何要修改对象的数据如何传递到表单的这些参数中的方式?

4

2 回答 2

1

您可以使用wtforms.fields.FormField使用嵌套表单。在你的情况下,它会是这样的:

class CitiForm(Form):
    adapt = TextField(_('stream adapt link'))
    hds = TextField(_('stream hds link'))
    hls = TextField(_('stream hls link'))
    rtmp = TextField(_('stream rtmp link'))

class ChannelForm(Form):
    _id = HiddenField()
    name = TextField(_('channel name'))
    city1 = FormField(CitiForm)
    city2 = FormField(CitiForm)
于 2012-10-07T01:05:17.303 回答
1

在文档中很好地找到了这一点,我现在将使用它:

在视图中:

channel_obj = db.TVChannel().get_id(channel_id) #load a channel's infos into an object
channel     = ChannelForm(request.form, obj=channel_obj) #load channel form
channel.CITY1adapt.process_data(channel_obj.streams['City1']['adapt'])
#and others links

并以以下形式:

class ChannelForm(Form):    
    _id         = HiddenField()
    name        = TextField(_('channel name'))    
    CITY1adapt  = TextField(_('adapt link')) 
    CITY2adapt  = TextField(_('adapt link'))
    #and so on

    submit      = SubmitField(_('Save'))

现在,当我“保存”它们时,我正在努力。

于 2012-10-08T04:37:17.400 回答