Эй, я новичок в Flask и пытаюсь создать базовое c приложение для викторины,
@app.route('/answers', methods=['GET', 'POST'])
def answers():
correct = 0
incorrect = 0
total = correct + incorrect
if request.method == 'POST':
submitted_answer = request.form['answer']
if submitted_answer == question_option1:
correct += 1
new_result = Question(correct=correct, total=total)
db.session.add(new_result)
db.session.commit()
else:
incorrect += 1
new_result = Question(incorrect=incorrect, total=total)
db.session.add(new_result)
db.session.commit()
return redirect('/answers')
all_questions = Question.query.order_by().all()
return render_template('answers.html', questions=all_questions, correct=correct, incorrect=incorrect, total=total)
, в то время как код html выглядит следующим образом
{% extends 'base.html' %}
{% block body %}
<p1>
<list>
{% for question in questions %}
<h2>{{ question.text }}</h2>
<input type="radio" name="answer" value="option1"> {{ question.option1 }} <br>
<input type="radio" name="answer" value="option2"> {{ question.option2 }} <br>
<input type="radio" name="answer" value="option3"> {{ question.option3 }} <br>
<input type="radio" name="answer" value="option4"> {{ question.option4 }} <br>
{% endfor %}
</list>
<form action='/answers' method='POST'>
<input type="submit" name="submitted" value="Check Scores">
</form>
<p1>You got a score of {{ correct }}/{{ total }}</p1>
</p1>
{% endblock %}
В другой части моего приложения я могу получить информацию от HTML, используя тот же метод, но в этом случае я продолжаю получать ошибку
'werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: браузер (или прокси) отправил запрос, который этот сервер не может понять. KeyError: 'answer' '
, когда я пытаюсь отправить контрольный список и не могу понять, почему
Я новичок в программировании, так что извините, если я что-то упустил очень простой c но спасибо за помощь!