我有两个烧瓶表单来在我的应用程序上创建书籍和作者。它们只有在所有字段都填满时才有效,而我只需要严格要求的一两个!
作者类:
class AuthorForm(Form):
familyname = TextField('familyname', validators = [Required()])
firstname = TextField('firstname', validators = [Required()])
biography = TextAreaField('biography')
dateofbirth = DateField('dateofbirth')
placeofbirth = TextField('placeofbirth')
nationality = TextField('nationality')
website = TextField('website')
模板:
<form action="" method="post" name="add_author">
{{form.hidden_tag()}}
<p>Veuillez donner son prénom (obligatoire) : {{form.firstname}}</p>
<p>Nom de famille (obligatoire) : {{form.familyname}}</p>
<p>Nationalité : {{form.nationality}}</p>
<p>Lieu de naissance : {{form.placeofbirth}}</p>
<p>Date de naissance (Y-m-d): {{form.dateofbirth}}</p>
<p>Site web : {{form.website}}</p>
<p>Biographie : {{form.biography}}</p>
<p><input type="submit" value="Envoyer"></p>
</form>
{% endblock %}
和观点:
@app.route('/add_author', methods = ['GET', 'POST'])
def add_author():
form = AuthorForm()
if form.validate_on_submit():
a = Author(firstname= form.firstname.data,
familyname= form.familyname.data,
nationality= form.nationality.data,
dateofbirth= form.dateofbirth.data,
placeofbirth = form.placeofbirth.data,
website = form.website.data,
biography = form.biography.data)
db.session.add(a)
db.session.commit()
return redirect('/index')
return render_template('add_author.html', title = 'Ajouter un auteur a la base de donnees', form = form)