1. Request
HTTP
works as a request-response protocol between a client and server.
To access incoming request data, you can use the global request
object. Flask parses incoming request data for you and gives you access to it through that global object. Internally Flask makes sure that you always get the correct data for the active thread if you are in a multithreaded environment. Some examples of how to get request
data: request.form
, request.args.get
, request.files
.
2. GET and POST
POST
: The browser tells the server that it wants to post some new information to that URL and that the server must ensure the data is stored and only stored once. This is how HTML forms usually transmit data to the server.
PUT
: Similar to POST
but the server might trigger the store procedure multiple times by overwriting the old values more than once. Now you might be asking why this is useful, but there are some good reasons to do it this way. Consider that the connection is lost during transmission: in this situation a system between the browser and the server might receive the request safely a second time without breaking things. With POST
that would not be possible because it must only be triggered once.
3. form
The form
tag needs two attributes set:
3.1. form action
action
is where the data is sent. If action is omitted, it assumed to be the current page (the same URL). In flask
, action
is the URL that the form data is sent to on submit. Generate it with url_for. It can be omitted if the same URL handles showing the form and processing the data.
Add a view to handle the form data:
@app.route('/handle_data', methods=['POST'])
def handle_data():
projectpath = request.form['projectFilepath']
# your code
# return a response
Set the form's action to that view's URL:
<form action="{{ url_for('handle_data') }}" method="post">
<input type="text" name="projectFilepath">
<input type="submit">
</form>
3.2. form method
method
: Whether to submit the data as a query string (GET) or form data (POST).
Reference
- HTTP Methods: GET vs. POST
- How can I pass arguments into redirect(url_for()) of Flask?
- How to get the name of a submitted form in Flask?
- Sending data from HTML form to a Python script in Flask
- HTTP Methods
- What is the difference between action and target of post?
- Incoming Request Data
- How to get data received in Flask request