HTTP is stateless so to transfer data between different pages in a web application it is passed through URLs and accessed via request object. Flask request GET and POST are the HTTP methods to receive data from other pages.
In a Flask application routes are created to link different sections of a web application. These routes can accept the data sent by using the appropriate request object methods. By default the GET method is used, but you can use the POST method by mentioning it in the route decorator in the methods argument. We will discuss how and where to use the Flask request get and post methods.
Flask Request get
HTTP get method sends unencrypted data to the target script through URL for processing. The syntax of the route decorator to use get method is –
@(object instance of Flask application).route(‘url to be defined of the page to be linked’, methods=[‘GET’])
When Flask request get is used the form data is passed on to the requested script in form action through URL as arguments. The data is collected from the form and send along with the URL as parameter and value pairs. These values can be accessed in the requested script by using request.args and passing the names of the parameter. The syntax is –
Variable-name= request.args.get(‘form-parameter-name’)
Example
Application.py
@app.route("/") def index(): return render_template("index.html") @app.route('/quiz', methods=["GET"]) def quiz(): subject= request.args.get('sub') return render_template("quiz.html",subject=subject)
Index.html
<form method="GET" action={{url_for('quiz')}}> <div>Choose a Subject</div><br> <div class="subject"> <input type="radio" name="sub" value="Data Structure">Data Structures</input> </div> <div class="subject"> <input type="radio" name="sub" value="Operating System">Operating Systems</input> </div> <input type="submit"> </form>
Quiz.html
{% block body %} you have chosen {{subject}} {% endblock %}
Flask Request post
HTTP post method sends encrypted data to the requested script. The syntax of the route decorator is –
@(object instance of Flask application).route(‘url to be defined of the page to be linked’, methods=[‘POST’])
When Flask request post is used the form data is collected from the requesting form and passed on to the script. This data is not sent along with URL as arguments. This sent data is not visible in the URL. These values can be accessed in the script by using request.form.get passing the name of the parameter whose data you want to access. The syntax is –
Variable-name= request.form.get(‘form-parameter-name’)
Example
Application.py
@app.route("/") def index(): return render_template("index.html") @app.route('/quiz', methods=["POST"]) def quiz(): subject= request.form.get('sub') return render_template("quiz.html",subject=subject)
Index.html
<form method="POST" action={{url_for('quiz')}}> <div>Choose a Subject</div></br> <div class="subject"> <input type="radio" name="sub" value="Data Structure">Data Structures</input> </div> <div class="subject"> <input type="radio" name="sub" value="Operating System">Operating Systems</input> </div> <input type="submit"> </form>
Quiz.html
{% block body %} you have chosen {{subject}} {% endblock %}
You can use both POST and GET in the route decorator in the methods part. Irrespective of what method is used in the HTML form, the appropriate method is used in the route.
Be First to Comment