0
0
Flaskframework~30 mins

Accessing form data in routes in Flask - Mini Project: Build & Apply

Choose your learning style9 modes available
Accessing form data in routes
📖 Scenario: You are building a simple web app where users can submit their favorite fruit through a form.
🎯 Goal: Create a Flask app that receives form data from a POST request and extracts the submitted fruit name in the route.
📋 What You'll Learn
Create a Flask app with a route to display a form
Add a form with a single input named 'fruit'
Create a POST route to receive the form submission
Access the form data in the route using Flask's request object
💡 Why This Matters
🌍 Real World
Web apps often need to collect user input through forms and process it on the server.
💼 Career
Understanding how to access form data in routes is essential for backend web development with Flask.
Progress0 / 4 steps
1
Set up Flask app and form route
Write code to import Flask and create an app instance called app. Then define a route / that returns a simple HTML form with a single input named fruit and a submit button.
Flask
Need a hint?

Remember to import Flask and create the app instance. The form should use POST method and have an input named 'fruit'.

2
Add POST route to receive form data
Add a new route /submit that accepts only POST requests. Import request from Flask to access form data.
Flask
Need a hint?

Use @app.route with methods=['POST'] to create the POST route.

3
Access the form data in the POST route
Inside the submit function, use request.form to get the value of the input named fruit and store it in a variable called fruit_name.
Flask
Need a hint?

Use request.form['fruit'] to get the submitted fruit value.

4
Return a response showing the submitted fruit
Complete the submit function by returning a string that says "You submitted: " followed by the value of fruit_name.
Flask
Need a hint?

Use an f-string to include fruit_name in the returned string.