post in python flask

Post in Python Flask

If you are looking to create a web application using Python, Flask is one of the best frameworks to use. It is lightweight, flexible, and easy to learn. In this article, I will explain how to post data from a form in Flask.

Creating a Form in HTML

Before we can post data in Flask, we need to create a form in HTML. We can use the following code:


            <form method="POST" action="/submit">
                <input type="text" name="name">
                <input type="submit" value="Submit">
            </form>
        

In this form, we have used the POST method and set the action to "/submit". We have also added a text input field with the name "name" and a submit button.

Handling the Form Data

Now that we have created our form, we need to handle the data when the form is submitted. We can use the following code in our Flask application:


            from flask import Flask, request

            app = Flask(__name__)

            @app.route('/submit', methods=['POST'])
            def submit():
                name = request.form['name']
                return 'Name: {}'.format(name)
        

In this code, we have created a route for "/submit" with the POST method. When the form is submitted, the data will be sent to this route. We have also used the request object to get the form data and stored it in a variable called "name". Finally, we have returned the name in a string format.

Multiple Ways to Handle Form Data

There are multiple ways to handle form data in Flask. We can use the "request.form.get" method instead of "request.form['name']". We can also use the Flask-WTF extension to validate the form data. The following code shows how to use Flask-WTF:


            from flask import Flask, render_template
            from flask_wtf import FlaskForm
            from wtforms import StringField, SubmitField
            from wtforms.validators import DataRequired

            app = Flask(__name__)
            app.config['SECRET_KEY'] = 'secret key'

            class NameForm(FlaskForm):
                name = StringField('Name', validators=[DataRequired()])
                submit = SubmitField('Submit')

            @app.route('/submit', methods=['GET', 'POST'])
            def submit():
                form = NameForm()
                if form.validate_on_submit():
                    name = form.name.data
                    return 'Name: {}'.format(name)
                return render_template('form.html', form=form)
        

In this code, we have created a form using the Flask-WTF extension. We have added a StringField for the name with a validator for required data. We have also added a SubmitField. In the submit function, we have created an instance of the form and checked if it was validated. If it was validated, we have returned the name. If not, we have rendered the form template with the form instance.

Conclusion

In conclusion, posting data in Flask is easy and flexible. We can create a form in HTML and handle the data using the request object or use the Flask-WTF extension for form validation. The possibilities are endless with Flask.