0
0
Flaskframework~10 mins

CRUD operations in Flask - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Flask class from the flask module.

Flask
from flask import [1]
app = Flask(__name__)
Drag options to blanks, or click blank then click option'
AFlask
Bjsonify
Crender_template
DRequest
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request instead of Flask
Using lowercase flask instead of Flask
2fill in blank
medium

Complete the code to define a route for the home page that returns 'Hello World!'.

Flask
@app.route('/')
def home():
    return [1]
Drag options to blanks, or click blank then click option'
A'Hello World!'
Bjsonify(message='Hello')
Crender_template('index.html')
D'Welcome'
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a function call instead of a string
Forgetting quotes around the string
3fill in blank
hard

Fix the error in the code to get JSON data from a POST request.

Flask
from flask import request

@app.route('/data', methods=['POST'])
def data():
    json_data = request.[1]
    return json_data
Drag options to blanks, or click blank then click option'
Ajsonify
Bget_json
Cjson
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Calling get_json() on request.json which is not a method
Using jsonify instead of accessing request.json
4fill in blank
hard

Fill both blanks to create a route that updates a user by ID using PUT method.

Flask
@app.route('/user/<int:id>', methods=[[1]])
def update_user(id):
    data = request.[2]
    # update logic here
    return 'User updated'
Drag options to blanks, or click blank then click option'
A'PUT'
B'POST'
Cjson
Dform
Attempts:
3 left
💡 Hint
Common Mistakes
Using POST instead of PUT for update
Accessing request.form instead of request.json
5fill in blank
hard

Fill all three blanks to create a route that deletes a user by ID and returns JSON confirmation.

Flask
@app.route('/user/<int:id>', methods=[[1]])
def delete_user(id):
    # delete logic here
    return [2]({'message': 'User deleted', 'id': [3])
Drag options to blanks, or click blank then click option'
A'DELETE'
Bjsonify
Cid
D'GET'
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET instead of DELETE
Returning plain dict instead of jsonify
Not including the id in the response