Handling forms in a Flask application involves several steps:
- Creating the HTML form in the template
- Handling the form submission in the view function
- Validating the form data
- Handling the form data and performing any necessary actions (such as storing the data in a database)
To create an HTML form in a Flask application, you can use standard HTML form elements such as <input>
, <select>
, and <textarea>
. You can also use a template engine such as Jinja2 to generate the form in your template. Here is an example of a simple HTML form:
<form action="{{ url_for('submit') }}" method="POST">
<label>Name: <input type="text" name="name"></label>
<label>Email: <input type="email" name="email"></label>
<input type="submit" value="Submit">
</form>
In this example, the action
attribute of the <form>
element specifies the URL that the form data should be sent to, and the method
attribute specifies the HTTP method (in this case, POST) that should be used to submit the form. The name
attribute of the <input>
elements specifies the name of the form fields, which will be used to retrieve the form data in the view function.
To handle the form submission in a view function, you can use the request.form
dictionary to access the form data. Here is an example of a view function that handles a form submission:
@app.route('/submit', methods=['POST'])
def submit():
name = request.form['name']
email = request.form['email']
return 'Name: {} Email: {}'.format(name, email)
view function uses the request.form
dictionary to access the name
and email
fields of the form and returns a message with the form data.
Once the form data is received by the view function, it is important to validate the data before performing any actions. This can be done using various libraries such as WTForms, Flask-WTF, etc. These libraries provide classes for creating forms, validating form data, and generating HTML form fields.
For example, using Flask-WTF, you can create a form class that defines the fields of the form and the validators to be used:
from flask_wtf import FlaskForm
from wtforms import StringField, validators
class ContactForm(FlaskForm):
name = StringField('Name', [validators.DataRequired()])
email = StringField('Email', [validators.DataRequired(), validators.Email()])
In this example, the ContactForm
class defines two fields, name
and email
, and specifies that the name
field is required and the email
field is required and must be a valid email address.
Once the form class is defined, you can use it to render the form in the template and validate the form data in the view function:
@app.route('/submit', methods=['POST'])
def submit():
form = ContactForm()
if form.validate_on_submit():
name = form.name.data
email = form.email.data
return 'Name: {} Email: {}'.format(name, email)
return render_template('form.html', form=form)
In this example, the view function first creates an instance of the ContactForm
class. Then, it checks if the form data is valid by calling the validate_on_submit()
method of the form instance. If the form data is valid, the view function retrieves the name
and email
data from the form and returns a message with the form data. If the form data is not valid, the view function renders the form.html
template with the form instance.
Once the form data is validated, you can use it to perform any necessary actions such as storing the data in a database. You can use an ORM library such as SQLAlchemy to handle the database interactions.
In summary, handling forms in a Flask application involves creating the HTML form in the template, handling the form submission in the view function, validating the form data, and performing any necessary actions with the form data. This can be accomplished by using standard HTML form elements, template engines, and libraries such as WTForms and Flask-WTF for handling forms and validations.