Flask request.form: Access POST Data in Python
In Python, request.form is a dictionary used to store form data from an HTTP request. It is an object in the Flask framework used to retrieve form data from a POST request.
When clients submit form data using the POST method, you can use request.form to retrieve this data. It can be used to handle user-submitted form data, including text, files, and other types of data. You can access the values of form fields by accessing key-value pairs in the dictionary.
Here is a simple example demonstrating how to use request.form to retrieve form data.
from flask import Flask, request
app = Flask(__name__)
@app.route('/submit', methods=['POST'])
def submit_form():
name = request.form.get('name')
email = request.form.get('email')
message = request.form.get('message')
# 处理表单数据
# ...
return '表单提交成功'
if __name__ == '__main__':
app.run()
In the example above, when a client submits form data to the /submit path using the POST method, the server will retrieve the values of the name, email, and message fields from request.form and process them accordingly.