【发布时间】:2022-08-15 09:16:30
【问题描述】:
我正在尝试实现一个表单,该表单收集音乐会场地上的数据,并使用 wtforms 和 flask-sqlalchemy orm 将其存储在我的本地 postgres 数据库中。
我注意到我的表单无法验证阻止我将新记录添加到我的数据库中。请看下面的相关代码
表格模型
class VenueForm(FlaskForm):
name = StringField(
\'name\', validators=[DataRequired()]
)
city = StringField(
\'city\', validators=[DataRequired()]
)
state = SelectField(
\'state\', validators=[DataRequired()],
choices=[
(\'AL\', \'AL\'),
(\'AK\', \'AK\'),
(\'AZ\', \'AZ\'),
(\'AR\', \'AR\'),
(\'CA\', \'CA\'),
(\'CO\', \'CO\'),
(\'CT\', \'CT\'),
(\'DE\', \'DE\'),
(\'DC\', \'DC\'),
(\'FL\', \'FL\'),
(\'GA\', \'GA\'),
(\'HI\', \'HI\'),
(\'ID\', \'ID\'),
(\'IL\', \'IL\'),
(\'IN\', \'IN\'),
(\'IA\', \'IA\'),
(\'KS\', \'KS\'),
(\'KY\', \'KY\'),
(\'LA\', \'LA\'),
(\'ME\', \'ME\'),
(\'MT\', \'MT\'),
(\'NE\', \'NE\'),
(\'NV\', \'NV\'),
(\'NH\', \'NH\'),
(\'NJ\', \'NJ\'),
(\'NM\', \'NM\'),
(\'NY\', \'NY\'),
(\'NC\', \'NC\'),
(\'ND\', \'ND\'),
(\'OH\', \'OH\'),
(\'OK\', \'OK\'),
(\'OR\', \'OR\'),
(\'MD\', \'MD\'),
(\'MA\', \'MA\'),
(\'MI\', \'MI\'),
(\'MN\', \'MN\'),
(\'MS\', \'MS\'),
(\'MO\', \'MO\'),
(\'PA\', \'PA\'),
(\'RI\', \'RI\'),
(\'SC\', \'SC\'),
(\'SD\', \'SD\'),
(\'TN\', \'TN\'),
(\'TX\', \'TX\'),
(\'UT\', \'UT\'),
(\'VT\', \'VT\'),
(\'VA\', \'VA\'),
(\'WA\', \'WA\'),
(\'WV\', \'WV\'),
(\'WI\', \'WI\'),
(\'WY\', \'WY\'),
]
)
address = StringField(
\'address\', validators=[DataRequired()]
)
phone = StringField(
\'phone\'
)
image_link = StringField(
\'image_link\'
)
genres = SelectMultipleField(
# TODO implement enum restriction
\'genres\', validators=[DataRequired()],
choices=[
(\'Alternative\', \'Alternative\'),
(\'Blues\', \'Blues\'),
(\'Classical\', \'Classical\'),
(\'Country\', \'Country\'),
(\'Electronic\', \'Electronic\'),
(\'Folk\', \'Folk\'),
(\'Funk\', \'Funk\'),
(\'Hip-Hop\', \'Hip-Hop\'),
(\'Heavy Metal\', \'Heavy Metal\'),
(\'Instrumental\', \'Instrumental\'),
(\'Jazz\', \'Jazz\'),
(\'Musical Theatre\', \'Musical Theatre\'),
(\'Pop\', \'Pop\'),
(\'Punk\', \'Punk\'),
(\'R&B\', \'R&B\'),
(\'Reggae\', \'Reggae\'),
(\'Rock n Roll\', \'Rock n Roll\'),
(\'Soul\', \'Soul\'),
(\'Other\', \'Other\'),
]
)
facebook_link = StringField(
\'facebook_link\', validators=[URL()]
)
website_link = StringField(
\'website_link\'
)
seeking_talent = BooleanField( \'seeking_talent\' )
seeking_description = StringField(
\'seeking_description\'
)
控制器
@app.route(\'/venues/create\', methods=[\'POST\'])
def create_venue_submission():
newForm = VenueForm()
if newForm.validate_on_submit():
venue = Venue(
name = newForm.name.data,
city = newForm.city.data,
state = newForm.state.data,
address =newForm.address.data,
phone = newForm.phone.data,
image_link =newForm.image_link.data,
genres = newForm.genres.data,
facebook_link = newForm.facebook_link.data,
seeking_description = newForm.seeking_description.data,
seeking_talent = newForm.seeking_talent.data,
website = newForm.website_link.data
)
db.session.add(venue)
db.session.commit()
flash(\'The Venue \' + newForm.name.data + \' has successfully been listed\')
else:
db.session.rollback()
flash(\'An error occurred. Venue \' + newForm.name.data + \' could not be listed.\')
db.session.close()
return render_template(\'pages/home.html\')
html
<form method=\"post\" id=\"venForm\" class=\"form\" action=\"/venues/create\">
{{ form.csrf_token }}
{% if form.csrf_token.errors %}
<div class=\"warning\">You have submitted an invalid CSRF token</div>
{% endif %}
<h3 class=\"form-heading\">List a new venue <a href=\"{{ url_for(\'index\') }}\" title=\"Back to homepage\"><i class=\"fa fa-home pull-right\"></i></a></h3>
<div class=\"form-group\">
<label for=\"name\">Name</label>
{{ form.name(class_ = \'form-control\', autofocus = true) }}
</div>
<div class=\"form-group\">
<label>City & State</label>
<div class=\"form-inline\">
<div class=\"form-group\">
{{ form.city(class_ = \'form-control\', placeholder=\'City\', autofocus = true) }}
</div>
<div class=\"form-group\">
{{ form.state(class_ = \'form-control\', placeholder=\'State\', autofocus = true) }}
</div>
</div>
</div>
<div class=\"form-group\">
<label for=\"address\">Address</label>
{{ form.address(class_ = \'form-control\', autofocus = true) }}
</div>
<div class=\"form-group\">
<label for=\"phone\">Phone</label>
{{ form.phone(class_ = \'form-control\', placeholder=\'xxx-xxx-xxxx\', autofocus = true) }}
</div>
<div class=\"form-group\">
<label for=\"genres\">Genres</label>
<small>Ctrl+Click to select multiple</small>
{{ form.genres(class_ = \'form-control\', placeholder=\'Genres, separated by commas\', autofocus = true) }}
</div>
<div class=\"form-group\">
<label for=\"facebook_link\">Facebook Link</label>
{{ form.facebook_link(class_ = \'form-control\', placeholder=\'http://\', autofocus = true) }}
</div>
<div class=\"form-group\">
<label for=\"image_link\">Image Link</label>
{{ form.image_link(class_ = \'form-control\', placeholder=\'http://\', autofocus = true) }}
</div>
<div class=\"form-group\">
<label for=\"website_link\">Website Link</label>
{{ form.website_link(class_ = \'form-control\', placeholder=\'http://\', autofocus = true) }}
</div>
<div class=\"form-group\">
<label for=\"seeking_talent\">Looking for Talent</label>
{{ form.seeking_talent(placeholder=\'Venue\', autofocus = true) }}
</div>
<div class=\"form-group\">
<label for=\"seeking_description\">Seeking Description</label>
{{ form.seeking_description(class_ = \'form-control\', placeholder=\'Description\', autofocus = true) }}
</div>
<input type=\"submit\" value=\"Create Venue\" class=\"btn btn-primary btn-lg btn-block\">
</form>
每当我尝试创建一个场所时,if newForm.validate_on_Submit() 都没有完成,而 else 被执行
-
newForm.errors中有什么内容? -
@snakecharmerb 没有显示错误
标签: flask flask-sqlalchemy flask-wtforms wtforms