Python 使用SQLAlchemy对象中的数据预填充烧瓶中的WTforms
我是flask框架的新手,当时正在为webportal创建一个编辑配置文件页面。我被卡在某个点上,无法自动填写表格 这是我的表格课:Python 使用SQLAlchemy对象中的数据预填充烧瓶中的WTforms,python,flask,flask-sqlalchemy,flask-wtforms,Python,Flask,Flask Sqlalchemy,Flask Wtforms,我是flask框架的新手,当时正在为webportal创建一个编辑配置文件页面。我被卡在某个点上,无法自动填写表格 这是我的表格课: class EditProfile(Form): username = TextField('Username', [Required()]) email = TextField('Email', [Required()]) about = TextAreaField('About', [Required()]) website
class EditProfile(Form):
username = TextField('Username', [Required()])
email = TextField('Email', [Required()])
about = TextAreaField('About', [Required()])
website = TextField('Website', [Required()])
这是我计算表单的函数
def editprofile(nickname = None):
if g.fas_user['username'] == nickname or request.method == 'POST':
form = EditProfile()
form_action = url_for('profile.editprofile')
if request.method == 'POST' and form.validate():
if form.username.data == nickname :
query = EditProfile(form.username.data,
form.email.data,
form.about.data,
form.website.data,
)
print query #debug
db.session.add(query)
db.session.commit()
flash('User Updated')
print "added"
return(url_for('profile.editprofile'))
return render_template('profile/add.html', form=form,
form_action=form_action, title="Update Profile")
else:
return "Unauthorised"
我的表单html模板是表单是:
{% extends "base.html" %}
{% block title %}
{{ title }}
{% endblock %}
{% block content %}
{% from "_formhelpers.html" import render_field %}
<div id="Edit Profile">
<h2>{{ title }}</h2>
<form method="post" action="{{ form_action }}">
<fieldset>
<legend></legend>
{{ render_field(form.username) }}
{{ render_field(form.email)}}
{{ render_field(form.about )}}
{{ render_field(form.website) }}
</fieldset>
<input type="submit" class="button" value="Save"/>
</form>
</div>
{% endblock %}
{%extends“base.html”%}
{%block title%}
{{title}}
{%endblock%}
{%block content%}
{%来自“\u formhelpers.html”导入呈现\u字段%}
{{title}}
{{render_字段(form.username)}
{{render_字段(form.email)}
{{render_field(form.about)}
{{render_field(form.website)}
{%endblock%}
我有一个用户类的对象。我想从这个对象中预先填充这个表单。如何在表单中预填充值。我试图在这里实现编辑配置文件功能 创建表单时,需要将对象传递给表单
form = EditProfile(obj=user) # or whatever your object is called
你会遇到麻烦的
query = EditProfile(form.username.data,
form.email.data,
form.about.data,
form.website.data,
)
db.session.add(query)
它将创建EditProfile
表单的新实例。然后尝试将其添加到会话中。会议需要的是模型,而不是表格
相反,在验证表单后,可以将其值与对象关联
form.populate_obj(user) # or whatever your object is called
由于对象已加载,因此无需将其添加到会话中。您可以删除
db.session.add(query)
,只需调用db.session.commit()
我发现的最简单的方法是在get请求中填写表单字段
@decorator_authorized_user # This decorator should make sure the user is authorized, like @login_required from flask-login
def editprofile(nickname = None):
# Prepare the form and user
form = EditProfile()
form_action = url_for('profile.editprofile')
my_user = Users.get(...) # get your user object or whatever you need
if request.method == 'GET':
form.username.data = my_user.username
form.email.data = my_user.email
# and on
if form.validate_on_submit():
# This section needs to be reworked.
# You'll want to take the user object and set the appropriate attributes
# to the appropriate values from the form.
if form.username.data == nickname:
query = EditProfile(form.username.data,
form.email.data,
form.about.data,
form.website.data,
)
print query #debug
db.session.add(query)
db.session.commit()
flash('User Updated')
print "added"
return(url_for('profile.editprofile'))
return render_template('profile/add.html', form=form,
form_action=form_action, title="Update Profile")
这将设置函数,以便在get请求时返回预填充的表单。您必须重新编辑
表单下的部分。请在提交时验证。Dirn的回答建议了一些正确的操作。要使用SQLAlchemy对象填充表单,请使用:
form = EditProfile(obj=<SQLAlchemy_object>)
然后在文章中,您必须这样做才能从表单中填充SQLAchemy对象:
form.populate_obj(user)
user.site = form.website.data
db.session.commit()
我还是有点困惑。一旦我以表单形式发送我的用户对象,我想我也需要编辑模板,否则它将如何将正确的feild与模型关联?因为我的用户模型和表单类中的数据成员具有不同的名称。为什么它们不同?还有,你能给我们看看模型吗?这是我的模型。根据上面的EditProfile
表单和您在GitHub上的代码,这些字段在您的用户模型和表单中具有相同的名称。是的,我已经对名称进行了更改,现在可以使用了。但是怎样才能使他们的名字保持不同。我怎样才能重新填充MultiSelectField?我不想使用obj,我想使用字段名,但它对meI不起作用。我不知道为什么这个例子在2020年很难找到:^(这里是PalletsProject上的一个页面,关于几乎所有的CRUD,但是更新——将SQLA obj更新为WTForms对象怎么样?%^\
form.populate_obj(user)
user.site = form.website.data
db.session.commit()