from flask import Flask, render_template, flash, request, redirect, url_for
app = Flask(__name__)
app.secret_key = 'your_secret_key'
@app.route('/')
def index():
return render_template('index.html')
@app.route('/process_form', methods=['POST'])
def process_form():
if request.method == 'POST':
data = request.form['data']
flash(f'You entered: {data}')
return redirect(url_for('index'))
if __name__ == '__main__':
app.run(debug=True)
- Import necessary modules from the Flask framework.
- Create a Flask web application instance.
- Set a secret key for the application to enable the use of session.
- Define a route for the home page ('/'), which renders an 'index.html' template.
- Define a route for processing form submissions ('/process_form') with the POST method.
- Check if the request method is POST, retrieve the form data, and flash a message with the entered data.
- Redirect the user back to the home page after processing the form.
- Run the Flask application in debug mode if the script is executed directly.