Python Computer Science
Beginner
80 mins
Teacher/Student led
+40 XP
What you need:
Chromebook/Laptop/PC

Handling Requests

In this lesson, you'll explore how to manage GET and POST requests in Flask, crucial for creating interactive web applications. Follow step-by-step instructions to set up a project, build routes, handle forms, and process user data effectively.
Learning Goals Learning Outcomes Teacher Notes

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
Load previous activity

    1 - Introduction

    In this lesson, you'll learn how to handle GET and POST requests in Flask, which are essential for building interactive web applications.

    GET requests are used to retrieve data, while POST requests are used to send data to the server, like submitting a form. You'll build on your knowledge from previous Flask lessons by creating an app that manages both types of requests. Here's what you'll do:

    1. Set up a new project folder and environment.
    2. Create a basic Flask app with a GET route.
    3. Add a form to handle POST requests.
    4. Process data from POST requests.
    5. Test and debug your app.

    By the end, you'll understand how to manage user inputs and responses in a web backend.

    2 - Create a Project Folder

    To begin, create a dedicated folder for this lesson's project to keep everything organised.

    1. On your computer, create a new folder named 'FlaskRequests'. You can place it in your Documents folder or any preferred location.
    2. Open VS Code and go to File > Open Folder, then select the 'FlaskRequests' folder.

    With your project folder ready, you're set to create a terminal in the next step.


    3 - Create the Environment

    Create a virtual environment to manage your project's dependencies separately.

    1. Open the Command Palette: View > Command Palette.
    2. Type 'Python: Create Environment' and select it.
    3. Choose 'Venv' as the type.
    4. Select your installed Python version.
    5. VS Code will create a '.venv' folder in 'FlaskRequests'.

    4 - Create a New Terminal and Install Flask

    Now, open a terminal in VS Code to install Flask if it's not already installed.

    1. Go to the menu: View > Terminal
    2. This opens the Terminal panel at the bottom of VS Code.
    3. In the terminal, run: pip install flask
    4. Wait for the installation to complete.

    5 - Creating a Basic Flask App with GET

    Let's create a simple Flask app that handles a GET request.

    GET requests are the most common type of HTTP request. They are used to retrieve data from a server without changing anything on the server. For example, when you visit a website in your browser, you're usually making a GET request to fetch the page content. In Flask, we use GET requests to display information or forms to the user.

    In VS Code, create a file named 'app.py' in 'FlaskRequests'.

    Add the following code:

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def home():
        return 'Welcome to Handling Requests Lesson! This is a GET request.'

    Save the file.
    Run python -m flask run in the terminal.
    Visit http://127.0.0.1:5000/ in your browser to see the message.
    Stop the server with Ctrl+C.



    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of DigitalSkills.org. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more