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

发布于 2024-11-12 17:04:41 字数 1803 浏览 3 评论 0 原文

我有一个模板,允许用户编辑他们的用户信息。

<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>

我还有一个查看功能来处理用户的此类编辑。我当前使用的数据库是 MongoDB 以及 MongoKit 模块。到目前为止,我只能在视图函数中做到这一点,但运气不佳。

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)

我正在经历 populate_obj(obj) 为此目的。在这件事上我找不到太多帮助。我应该怎么做才能让 populate_obj() 正常工作?

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. The database I am currently using is MongoDB with the MongoKit module. I have only been able to do up to this so far in the view function, yet with no luck.

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. I couldn't find much help in this matter. What should I do in order to get populate_obj() working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不疑不惑不回忆 2024-11-19 17:04:41

UserForm 应该将 request.form 传递到其中,以使用 POST 请求中可用的值(如果有)填充它。

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

UserForm should have request.form passed into it to populate it with the values available in the POST request (if any).

form = UserForm(request.form, obj=user)
荭秂 2024-11-19 17:04:41

您是否使用 Flask-WTF ?如果是这样,请查看以下示例代码:

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

具体来说,您会:

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

底线,如果您使用Flask-WTF,我不确定你的问题是什么。如果您不使用 Flask-WTF,请使用 Flask-WTF

Are you using Flask-WTF? If so, check out the following sample code:

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. If you aren't using Flask-WTF, use Flask-WTF.

泛泛之交 2024-11-19 17:04:41

如果是 Flask-WTF,你可以像

form = UserForm(obj=user)

Thant 那样写,就可以了!

In case of Flask-WTF, you can write like

form = UserForm(obj=user)

Thant will work!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文