简体   繁体   English

使用 WTForms 的 populate_obj( ) 方法和 Flask 微框架

[英]Using WTForms' populate_obj( ) method with Flask micro framework

I have a template which allows the user to edit their user information.我有一个模板,允许用户编辑他们的用户信息。

<form method="post">
    <table>
        <tr>
            <td>Username:</td>
            <td>{{user['username']}}</td>
        </tr>
        <tr>
            <td>New Password:</td>
            <td> <input type="password" name="password"></td>
            <td>{% if form.password.errors %} {{form.password.errors}} {% endif %}<td>
        </tr>
        <tr>
            <td>Re-enter Password:</td>
            <td> <input type="password" name="confirm_password">
            </td>
        </tr>
        <input type='hidden' name='username' value="{{user['username']}}">
        <tr>
            <td><input type="submit" value="Submit"></td>
        </tr>
    </table>
</form>

I also have a view function for handling such edits by the user.我还有一个视图 function 用于处理用户的此类编辑。 The database I am currently using is MongoDB with the MongoKit module.我目前使用的数据库是带有MongoKit模块的MongoDB I have only been able to do up to this so far in the view function, yet with no luck.到目前为止,我只能在 function 视图中做到这一点,但没有运气。

def edit():
    username = request.args.get('user')
    user = User.find_one({'username':username}) # Is this a correct way of doing it?
    form = UserForm(**what should be placed here?**, obj=user)

    if request.method == 'POST' and form.validate():
        form.populate_obj(user)
        user.save()
        return 'updated'
    return render_template('edituser.html', form=form, user=user)

I am going through populate_obj(obj) for this purpose.为此,我正在通过populate_obj(obj) I couldn't find much help in this matter.在这件事上我找不到太多帮助。 What should I do in order to get populate_obj() working?我应该怎么做才能让populate_obj()工作?

UserForm should have request.form passed into it to populate it with the values available in the POST request (if any). UserForm应该将request.form传递给它,以使用 POST 请求中可用的值(如果有)填充它。

form = UserForm(request.form, obj=user)

Are you using Flask-WTF ?你在使用Flask-WTF吗? If so, check out the following sample code:如果是这样,请查看以下示例代码:

https://github.com/sean-/flask-skeleton/blob/master/skeleton/modules/aaa/views.py#L13 https://github.com/sean-/flask-skeleton/blob/master/skeleton/modules/aaa/views.py#L13

Specifically, you would:具体来说,您将:

def edit():
    form = UserForm()
    if form.validate_on_submit():
        # Commit your form data

Bottom line, if you're using Flask-WTF , I'm not sure what your question is.底线,如果您使用的是Flask-WTF ,我不确定您的问题是什么。 If you aren't using Flask-WTF , use Flask-WTF .如果您不使用Flask-WTF ,请使用Flask-WTF

In case of Flask-WTF, you can write like如果是 Flask-WTF,你可以这样写

form = UserForm(obj=user)

Thant will work! Thant会工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在FormField中使用Flask / WTForms中的populate_obj? - How to use populate_obj in Flask / WTForms with a FormField? Flask - 使用 populate_obj 后保存额外的字段 - Flask - saving extra field after using populate_obj 在 WTForms 中使用 FieldList、FormField 和 populate_obj 填充列表,并在客户端添加项目 - Populating a list using FieldList, FormField, and populate_obj in WTForms, with adding items client-side flask wtforms QuerySelectFeld form.populate_obj 字段未填充,如果填写错误,则会引发翻译 - flask wtforms QuerySelectFeld form.populate_obj fields not populated and if filled in error translate raised 使用 Flask-WTF 时从字典中填充 WTForms 表单 - Populate WTForms form from dictionary when using Flask-WTF 使用psycopg2使用数据库中的数据填充烧瓶中的表单(wtforms) - Populate form (wtforms) in flask with data from database using psycopg2 如何使用 Flask/WTForms 预填充复选框 - How to pre-populate checkboxes with Flask/WTForms 使用 WTForms 和 Flask 预填充编辑表单 - Pre-Populate an edit form with WTForms and Flask 使用 WTForms 填充 textarea 字段 - Populate textarea field using WTForms Flask / WTF / SQLAlchemy:使用QuerySelectMultipleField和Form.populate_obj() - Flask/WTF/SQLAlchemy: using QuerySelectMultipleField with Form.populate_obj()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM