我是flask框架的新手,曾为门户网站创建编辑配置文件页面。我陷入了困境,无法自动填写表格。
这是我的表格类:
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 %}
我有一个对象,属于用户类。我想从那个对象中预填充这个表单。我如何预填充表单中的值。我正在尝试在这里实现编辑配置文件的功能。
创建对象时需要将其传递给表单。
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请求时返回一个预填充的表单。您将不得不在form.validate_on_submit
下返工该部分。Dirn的回答提出了一些正确的做法
要用SQLAlchemy对象填充表单,请使用:
form = EditProfile(obj=<SQLAlchemy_object>)
如果表单字段由于任何原因(它们应该(与模型中的数据库列不匹配,那么表单类将使用kwargs:
**kwargs–如果formdata或obj都不包含字段的值,则表单会将匹配关键字参数的值分配给字段(如果提供(。
我发现这方面的一个用例是,如果您的表单包含引用多个模型的字段(例如,通过关系(;仅仅通过一个CCD_ 5是不够的。
因此,让我们举一个例子,假设在数据库模型中(对于user
(使用site
而不是website
(表单字段名称(。这样做是为了从SQLAlchemy对象填充表单:
form = EditProfile(obj=user, website=user.site)
然后在POST中,您必须这样做才能从表单中填充SQLAchemy对象:
form.populate_obj(user)
user.site = form.website.data
db.session.commit()