flask get data from html form
To retrieve data from an HTML form using Flask, you can follow these steps:
- Import the necessary modules:
from flask import Flask, render_template, request
Create an instance of the Flask class:
app = Flask(__name__)
Define a route and a function to handle the request:
@app.route('/submit', methods=['POST'])
def submit():
Inside the function, retrieve the form data:
name = request.form.get('name')
email = request.form.get('email')
message = request.form.get('message')
Process the data as required:
You can perform any necessary operations on the retrieved data, such as storing it in a database or performing validations.
Return a response or redirect to another page:
return 'Data received successfully!'
Run the Flask application:
if __name__ == '__main__':
app.run()
Note: Make sure to replace the form field names ('name', 'email', 'message') with the actual names used in your HTML form. Additionally, the route in step 3 should match the action attribute of the HTML form.