0
0
Flaskframework~10 mins

Why Flask contexts matter - Test Your Understanding

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 correctly.

Flask
from flask import [1]
app = [1](__name__)
Drag options to blanks, or click blank then click option'
Arender_template
BRequest
CBlueprint
DFlask
Attempts:
3 left
💡 Hint
Common Mistakes
Importing Request instead of Flask
Using render_template where Flask is needed
Forgetting to import Flask
2fill in blank
medium

Complete the code to access the current request object inside a view function.

Flask
from flask import Flask, [1]

app = Flask(__name__)

@app.route('/')
def home():
    user_agent = [1].headers.get('User-Agent')
    return f"Your agent is {user_agent}"
Drag options to blanks, or click blank then click option'
Arequest
Bsession
Cg
Durl_for
Attempts:
3 left
💡 Hint
Common Mistakes
Using session instead of request
Trying to access request without importing it
Confusing g with request
3fill in blank
hard

Fix the error in accessing the Flask application context variable.

Flask
from flask import Flask, current_app

app = Flask(__name__)

with app.[1]():
    print('App name:', current_app.name)
Drag options to blanks, or click blank then click option'
Arequest
Bapp_context
Ccurrent_app
Dsession
Attempts:
3 left
💡 Hint
Common Mistakes
Using request instead of app_context
Trying to use current_app without context
Not creating any context before accessing app.name
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that stores request header lengths only if the header exists.

Flask
from flask import request

header_lengths = {header: len(request.headers.get(header)) for header in ['User-Agent', 'Host'] if request.headers.get([1]) [2] None}
Drag options to blanks, or click blank then click option'
A'User-Agent'
B==
C!=
Dheader
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' in the condition
Using a header name not in the list
Not checking if header exists before len()
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that stores uppercase header names and their values if the value length is greater than 5.

Flask
from flask import request

filtered_headers = [1]: [2] for [3], [2] in request.headers.items() if len([2]) > 5
Drag options to blanks, or click blank then click option'
Aheader.upper()
Bheader
Cvalue
Drequest.headers
Attempts:
3 left
💡 Hint
Common Mistakes
Using request.headers directly as key or value
Not using .upper() for keys
Mixing variable names in comprehension