如何重新填充WTForms [英] How to repopulate WTForms

查看:84
本文介绍了如何重新填充WTForms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在WTForms中创建了一个带有编辑按钮的表单,该按钮使用户可以编辑表单中的先前数据.

so i have made a form in WTForms with an edit button which allows the user to edit the previous data in the form.

我遇到的问题是从form中获取新数据.当我执行request.form时,我得到以下信息:

The problem i have is fetching the new data from the form.when i do request.form i get the following:

ImmutableMultiDict([('csrf_token', u'20130702225444##3f1c28cecaf55dc0e441d9820dfb52bb6df3d200'), ('csrf_token', u'20130702225444##3f1c28cecaf55dc0e441d9820dfb52bb6df3d200'), ('csrf_token', u'20130702225444##3f1c28cecaf55dc0e441d9820dfb52bb6df3d200'), ('csrf_token', u'20130702225444##3f1c28cecaf55dc0e441d9820dfb52bb6df3d200'), ('location_name', u'b'), ('feed_url', u'bkj'), ('title', u'b'), ('url', u'b'), ('date_crawled', u'b'), ('content_url', u'b'), ('longitude', u'b'), ('latitude', u'b'), ('date_added', u'b'), ('types', u'b')])

我想要的是从上述输出中检索以下内容

what i want is to retrieve the following from the above output

('location_name', u'b'), ('feed_url', u'bkj'), ('title', u'b'), ('url', u'b'), ('date_crawled', u'b'), ('content_url', u'b'), ('longitude', u'b'), ('latitude', u'b'), ('date_added', u'b'), ('types', u'b')])

推荐答案

您已经完成了在WTForms中写出表单类的工作(而且我怀疑Flask-WTF,除非您没有故意使用CSRF.

You already did the work of writing out a form class in WTForms (and I suspect Flask-WTF, given that the crsf_token would not be there normally unless you're using CSRF on purpose).

所以这意味着您已经做了类似的事情:

so that means you already did something akin to:

class MyForm(Form):
    feed_url = TextField(...)
    # etc

然后您已经做了类似的事情

and then you already did something like

def my_view():
    form = MyForm(request.form)
    render('mytemplate.html', form=form)

现在您有了这个很棒的表单对象,请使用它!也就是说,访问form.dataform.feed_url.data包含数据类型强制数据的数据.此外,您可以在wtforms中使用验证逻辑来​​确保您没有任何不良数据.没有理由使用request.form,它是来自框架的原始输入.

Now that you have this awesome form object, use it! That is, access the data off either form.data or form.feed_url.data which contains datatype-coerced data. Furthermore, you can use the validation logic in wtforms to make sure you don't have any bad data. There's no reason to use request.form which is the raw input coming from your framework.

这将使您拥有类似的东西(请注意,这是一个虚构的伪框架的通用示例,您将需要为框架获取适当的调用名称):

This will land you with something like (note this is a generic example for an imaginary pseudo-framework, you will need to get the appropriate call names for your framework):

def edit_location(location_id):
    my_object = LocationInfo.get(location_id)
    form = MyForm(request.form, obj=my_object)
    if request.form and form.validate():
        # If we got here, we have POST data and the form validated.
        form.populate_obj(my_object) # Super cool magic!
        my_object.save()
        return redirect(...)

    # If we fall back to here, it means validation failed or we're
    # viewing the form for the first time.
    render('mytemplate.html', form=form)

我建议您阅读WTForms的崩溃课程,以及一些那里的其他文档可以更好地了解如何使用WTForms.

I recommend you read the Crash Course for WTForms and also some of the other docs on there for a better idea of how to use WTForms.

这篇关于如何重新填充WTForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆