在下面提供Form.py文件:
Form.py file:
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, validators
from wtforms.validators import DataRequired, Length
# Define QuoteForm below
class QuoteForm(FlaskForm):
qauthor = StringField("Quote Author", validators =[Length(min=3,max=100,message=("Field must be between 3 and 100 characters long")), DataRequired(message=("This field is required"))])
qstring = StringField("Quote", validators =[Length(min=3,max=200,message=("Field must be between 3 and 200 characters long")), DataRequired(message=("This field is required"))])
submit= SubmitField("Add Quote")
提供视图功能:
@app.route('/addquote/', methods=['GET', 'POST'])
def add_quote():
form = QuoteForm()
if request.method=='POST':
quote = Quotes(quoteauthor=form.qauthor.data, quotestring= form.qstring.data)
try:
db.session.add(quote)
db.session.commit()
except Exception:
db.session.rollback()
return render_template("addquote_confirmation.html",form=QuoteForm())
else:
return render_template("addquote.html",form=QuoteForm())
if __name__=="__main__":
app.run()
表单的HTML文件:
<form action = "on_validate()" method = "POST">
{{ form.qauthor.label }} : {{ form.qauthor }} <br>
{% for error in form.qauthor.errors %}
<span style="color: red;">[{{ error }}]</span><br>
{% endfor %} <br>
{{ form.qstring.label }} : {{ form.qstring }} <br><br>
{% for error in form.qstring.errors %}
<span style="color: red;">[{{ error }}]</span><br>
{% endfor %} <br>
{{ form.submit }}
</form>
运行测试用例时出现以下错误: AssertionError:在b'\ n \ n \ n \ n \ n \ n \ n
答案 0 :(得分:0)
没有on_validate()。在下面使用:
<form method = "POST" novalidate>
{{ form.qauthor.label }} : {{ form.qauthor }} <br>
{% for error in form.qauthor.errors %}
<span style="color: red;">[{{ error }}]</span><br>
{% endfor %} <br>
{{ form.qstring.label }} : {{ form.qstring }} <br><br>
{% for error in form.qstring.errors %}
<span style="color: red;">[{{ error }}]</span><br>
{% endfor %} <br>
{{ form.submit }}
</form>
for form.py
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, validators
from wtforms.validators import InputRequired, Length
# Define QuoteForm below
class QuoteForm(FlaskForm):
qauthor = StringField("Quote Author", validators =[Length(min=3,max=100,message=("Field must be between 3 and 100 characters long.")), InputRequired(message='This field is required.')])
qstring = StringField("Quote", validators =[Length(min=3,max=200,message=("Field must be between 3 and 200 characters long.")), InputRequired(message='This field is required.')])
submit= SubmitField("Add Quote")
和route.py
@app.route('/addquote/', methods=['GET', 'POST'])
def add_quote():
form = QuoteForm()
if form.validate_on_submit():
if request.method=='POST':
quote = Quotes(quoteauthor=form.qauthor.data, quotestring= form.qstring.data)
try:
db.session.add(quote)
db.session.commit()
except Exception:
db.session.rollback()
return render_template("addquote_confirmation.html",form=form)
else:
return render_template("addquote.html",form=form)
return render_template("addquote_confirmation.html",form=form)
else:
return render_template("addquote.html",form=form)