我对视图功能有疑问。我的其他代码正常工作并且具有相同的结构或逻辑。我只是想知道为什么在这种情况下,当逻辑与其余部分相同时它不起作用,而其余部分正常工作,这个却出现错误。
# ########### POST answers ###############################
if request.method == 'POST':
if form.validate_on_submit():
answer = request.form['answer_text']
name = form.name.data
subject = request.form['hidden_sub']
flash('Homework posted for ' + str(subject) + "!")
try:
cur = mysql.connection.cursor()
sql = '''INSERT INTO answers (email, school, name, answer, subject)
VALUES (%s, %s, %s, %s, %s)'''
cur.execute(sql, (session['user'], session['school'], name, answer, subject))
mysql.connection.commit()
finally:
cur = mysql.connection.cursor()
cur.close()
return render_template('s_homework.html', user=session['user'], school=session['school'],
classes=classes, infos=infos, info_size=info_size, form=form)
HTML FORM:
<form method="POST" action="/s_homework/{{user}}/{{school}}/">
<input type="text" value="{{sub}}" style="visibility:hidden;" name="hidden_sub">
{{ wtf.form_field(form.name) }}
<label> Answer Form*</label>
<textarea class="form-control" name="answer_text" style="height:10em;"></textarea>
<p> </p>
{{ wtf.form_field(form.submit, class="btn btn-success",
style="position:relative; left:88%;") }}
</form>