我正在开发一个测验应用程序,但是我无法从我有RadioButton的表单中获取答案。
相关代码: 这是我的html表单:
{% for answer in answers %}
<form method="POST" action="">
<div class="btn-group-lg" data-toggle="buttons">
<label class="btn btn-outline-info">
<input type="radio" name="options" id="option" class="btn btn-primary" value="Submit" required> {{answer.answer}}
</label>
</div>
</form>
{% endfor %}
在这里,我有.py代码,可以在其中创建我的RadioButton的表单,在其中可以创建根目录,并且可以使用我的功能:
class QuestionForm(Form):
radio_button = RadioField('option', choices=[])
question_list = []
answer_list = []
@app.route('/question/<int:index>/', methods=['GET','POST'])
def question(index):
questions = getQuestions()
for question in questions:
question_list.append(question['question_id'])
answers = getQuestionAnswers(question_list[index])
questions_lenght = len(questions)
form = QuestionForm()
if request.method == 'POST' and form.validate():
option = form.radio_button.data
print(option)
return render_template('question.html', question=questions[index]["question"], answers=answers, index=index, questions_lenght=questions_lenght, form=form)
return render_template('question.html', question=questions[index]["question"], answers=answers, index=index, questions_lenght=questions_lenght, form=form)
def getQuestions():
mysql = MySQLdb.connect(host="localhost",
user="",
passwd="",
db="",
cursorclass=MySQLdb.cursors.DictCursor)
cur = mysql.cursor()
cur.execute("SELECT * FROM questions")
questions = cur.fetchall()
cur.close()
return questions
def getQuestionAnswers( question_id ):
mysql = MySQLdb.connect(host="localhost",
user="",
passwd="",
db="",
cursorclass=MySQLdb.cursors.DictCursor)
cur = mysql.cursor()
cur.execute("SELECT * FROM answers WHERE question_id = %s", [question_id])
answers = cur.fetchall()
cur.close()
return answers
if __name__ == '__main__':
app.secret_key=''
app.run(debug=True)
我的问题出在以下代码块中:
if request.method == 'POST' and form.validate():
option = form.radio_button.data
print(option)
什么都没打印。