flask get data from html form

To retrieve data from an HTML form using Flask, you can follow these steps:

  1. Import the necessary modules:
  2. from flask import Flask, render_template, request

  3. Create an instance of the Flask class:

  4. app = Flask(__name__)

  5. Define a route and a function to handle the request:

  6. @app.route('/submit', methods=['POST'])
  7. def submit():

  8. Inside the function, retrieve the form data:

  9. name = request.form.get('name')
  10. email = request.form.get('email')
  11. message = request.form.get('message')

  12. Process the data as required:

  13. You can perform any necessary operations on the retrieved data, such as storing it in a database or performing validations.

  14. Return a response or redirect to another page:

  15. return 'Data received successfully!'

  16. Run the Flask application:

  17. 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.