Python中文
首页
教程
问答
标签
搜索
登录
注册
使用Flask微框架的WTForms的populate_obj()方法
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>我有一个模板,允许用户编辑他们的用户信息。</p> <pre><code><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> </code></pre> <p>我还有一个视图函数,用于处理用户的此类编辑。我当前使用的数据库是带有<a href="http://pypi.python.org/pypi/mongokit" rel="noreferrer">MongoKit</a>模块的<a href="http://en.wikipedia.org/wiki/MongoDB" rel="noreferrer">MongoDB</a>。到目前为止,我只能在view函数中完成这个任务,但没有运气。</p> <pre><code>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) </code></pre> <p>为此,我将通过<a href="http://wtforms.simplecodes.com/docs/0.6.1/forms.html#wtforms.form.Form.populate_obj" rel="noreferrer">populate_obj(obj)</a>。在这件事上我找不到什么帮助。我该怎么做才能让<code>populate_obj()</code>工作?</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p><code>UserForm</code>应该将<code>request.form</code>传递给它,以便用POST请求中可用的值(如果有的话)填充它。</p> <pre><code>form = UserForm(request.form, obj=user) </code></pre>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何在python中从数据帧列中删除分类值?
2 回答
如何在python中从数据帧列表中删除引号
3 回答
如何在python中从数据帧创建列表
4 回答
如何在Python中从数据帧创建嵌套的JSON
3 回答
如何在Python中从数据帧显示wordcloud
3 回答
如何在Python中从数据帧的时间戳中删除字符
6 回答
如何在Python中从数据帧绘制简单绘图?
8 回答
如何在python中从数据帧行提取具有特定长度的范围?
10 回答
如何在python中从数据帧设置dict中的值
6 回答
如何在Python中从数据库中获得一个结果
5 回答
如何在python中从数据框中绘制分类条形图
7 回答
如何在Python中从数据框中选择特定细节?
2 回答
如何在python中从数据集中删除unicode
5 回答
如何在python中从数据集中删除某些数值?
3 回答
如何在python中从数据集中选择行
10 回答
如何在Python中从数组中删除元素
7 回答
如何在python中从数组中删除单个倒逗号?
9 回答
如何在python中从数组中删除对象?
4 回答
如何在python中从数组中删除引号
6 回答
如何在python中从数组中删除所有最小值
8 回答