Answer...">

How to enter html in Flask?

I have this html bit:

<form action='quiz_answers'> <p> Question1? </p> <input type="radio" name="q1" value="2">Answer1</input> <input type="radio" name="q1" value="1">Answer2</input> <input type="radio" name="q1" value="0">Answer3</input> <input type="radio" name="q1" value="0">Answer4</input> <p> Question2? </p> <input type="radio" name="q2" value="2">Answer1</input> <input type="radio" name="q2" value="1">Answer2</input> <input type="radio" name="q2" value="0">Answer3</input> <input type="radio" name="q2" value="0">Answer4</input> </form> 

and this python code:

 from flask import Flask, render_template, request @app.route('/quiz') def quiz(): return render_template('quiz.html') @app.route('/quiz_answers', methods=['POST']) def quiz_answers(): q1 = request.form['q1'] q2 = request.form['q2'] q4 = request.form['q4'] q5 = request.form['q5'] if __name__ == "__main__": app.debug = True app.run(host='0.0.0.0') 

How can I add a button, after which after clicking on + question 1 and 2, an answer is received, a new template opens with the results? In short, how can I make the button "Yup, answer questions, count values ​​and return them to a new HTML page"?

The Flask quick start tutorial goes through HTTP requests, but does not answer my question in this particular situation. Googling only gave https://stackoverflow.com/a/2129609/ which did not bring me anywhere.

+6
source share
1 answer

You should be able to add a submit button to the form in POST or return a data response in action .

In this case, you probably want to change the definition of the form tag to:

 <form action="/quiz_answers" method="POST"> 

And add a submit button as follows:

 <input type="submit" value="Submit!" /> 

When the user clicks the button, he should return the POST request to http://your_server/quiz_answers .

+13
source

Source: https://habr.com/ru/post/955364/


All Articles