def new():
if request.method == 'POST':
# The request is POST with some data, get POST data and validate it.
# The form data is available in request.form dictionary.
# Check if all the fields are entered. If not, raise an error
if not request.form['name'] or not request.form['email'] or not request.form['comment']:
flash('Please enter all the fields', 'error')
else:
# The data is valid. So create a new 'Comments' object
# to save to the database
comment = Comments(request.form['name'],
request.form['email'],
request.form['comment'])
# Add it to the SQLAlchemy session and commit it to
# save it to the database
db.session.add(comment)
db.session.commit()
# Flash a success message
flash('Comment was successfully submitted')
# Redirect to the view showing all the comments
return redirect(url_for('show_all'))
# Render the form template if the request is a GET request or
# the form validation failed
return render_template('new.html')
# This is the code that gets executed when the current python file is
# executed.
评论列表
文章目录