[{"id":"flaskadvanced-001","courseId":"flask-advanced","domain":"routing-and-requests","topic":"url_value_preprocessor","prompt":"A url_value_preprocessor runs values.pop('lang_code', None) and stores the result on g. A request to /en/ reaches index(), which takes no arguments. What happens?","hint":"Think about what happens to the dictionary of URL values before the view is called.","explain":"A URL rule passes its captured values as a dictionary. Hooks that run after matching can take entries out of that dictionary before the view is called. Consider what the view's signature must then accept.","source":"https://flask.palletsprojects.com/en/stable/patterns/urlprocessors/#internationalized-application-urls","interaction":{"type":"single"},"options":[{"id":"a","text":"It returns normally; the view receives no lang_code argument.","why":"Once the value is popped from the dictionary, it is no longer forwarded to the view function."},{"id":"b","text":"It raises a TypeError, because the URL still carries lang_code.","whyNot":"The preprocessor removes the value before the view is called, so no extra argument is passed."},{"id":"c","text":"The view gets lang_code = 'en' as a keyword argument.","whyNot":"Popping removes the entry; g keeps the copy, and the view is not handed the value."},{"id":"d","text":"Flask returns 404, because the view's signature no longer matches the rule.","whyNot":"Matching happens before preprocessing; the rule has already matched when the processor runs."}],"answer":"a","explanation":"A url_value_preprocessor runs right after a request is matched; a value it removes from the URL dictionary is not forwarded to the view function.","concept":"A url_value_preprocessor runs right after a request is matched; a value it removes from the URL dictionary is not forwarded to the view function.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","code":"@app.url_value_preprocessor\ndef pull_lang(endpoint, values):\n    g.lang = values.pop('lang_code', None)\n\n@app.route('/<lang_code>/')\ndef index():\n    return 'ok'","takeaways":["A value popped in a url_value_preprocessor is hidden from the view's arguments."]},{"id":"flaskadvanced-002","courseId":"flask-advanced","domain":"routing-and-requests","topic":"Blueprint URL prefix variables","prompt":"A blueprint is created with url_prefix='/<lang_code>' and has its own url_defaults and url_value_preprocessor. Which TWO statements about this setup are correct?","hint":"Consider which hooks run for this blueprint's URLs, and what the view receives once they have run.","explain":"A blueprint can carry its own URL hooks, and a variable in its prefix is an ordinary URL value. Think about what the hooks must do when a URL is built, and after a request matches.","source":"https://flask.palletsprojects.com/en/stable/patterns/urlprocessors/#internationalized-blueprint-urls","interaction":{"type":"multiple","tellCount":true},"options":[{"id":"a","text":"The blueprint's url_defaults can set lang_code without checking whether each endpoint expects it.","why":"A per-blueprint url_defaults no longer has to check whether the URL is interested in a 'lang_code' parameter, which removes a lot of logic."},{"id":"b","text":"The blueprint's url_value_preprocessor runs before the URL is matched, so it sees the raw path.","whyNot":"Preprocessors run right after the request has been matched, so the URL values already exist."},{"id":"c","text":"After the preprocessor pops lang_code, the blueprint's views receive no lang_code argument.","why":"A value popped from the URL values is not forwarded to the view function."},{"id":"d","text":"Views in this blueprint must still set g.lang_code themselves, because the prefix only affects routing.","whyNot":"The preprocessor exists so the assignment to g no longer has to be repeated in every function."},{"id":"e","text":"The '<lang_code>' part of url_prefix is a fixed string, so every URL in the blueprint shares one language.","whyNot":"It is a URL variable, so each request can carry its own value."},{"id":"f","text":"The url_defaults function overwrites any lang_code the caller passed to url_for.","whyNot":"setdefault only fills in the key when it is missing, so a value the caller passed is kept."}],"answer":"ac","explanation":"Per-blueprint URL processors skip the endpoint check that app-level url_defaults needs, and a URL value popped by a preprocessor is not passed on to the view.","concept":"Per-blueprint URL processors skip the endpoint check that app-level url_defaults needs, and a URL value popped by a preprocessor is not passed on to the view.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","code":"bp = Blueprint('frontend', __name__,\n    url_prefix='/<lang_code>')\n\n@bp.url_defaults\ndef add_language_code(endpoint, values):\n    values.setdefault(\n        'lang_code', g.lang_code)\n\n@bp.url_value_preprocessor\ndef pull_lang_code(endpoint, values):\n    g.lang_code = values.pop('lang_code')","takeaways":["Blueprint-level URL processors keep prefix logic out of app-wide hooks."]},{"id":"flaskadvanced-003","courseId":"flask-advanced","domain":"routing-and-requests","topic":"View.dispatch_request()","prompt":"A class-based view sets decorators = [cache(minutes=2), login_required]. Put these steps of applying those decorators by hand, first to last.","hint":"Each step takes the result of the step before it.","explain":"A decorator takes a function and returns a wrapper around it, so it is applied to whatever the previous step produced. The direction of wrapping decides which layer a request meets first.","source":"https://flask.palletsprojects.com/en/stable/views/#basic-reusable-view","interaction":{"type":"sort","items":[{"text":"view = UserList.as_view('users_list')","why":"The class becomes a plain view function first; no decorator has wrapped it yet."},{"text":"view = cache(minutes=2)(view)","why":"The first decorator in the list wraps the plain function, so it ends up innermost."},{"text":"view = login_required(view)","why":"The next decorator wraps the cached function, so it ends up outside the cache."},{"text":"app.add_url_rule('/users/', view_func=view)","why":"Only the fully wrapped function is registered with the app."}]},"options":[],"answer":"o:abcd","explanation":"Decorators in a view's decorators list are applied by hand as nested wrappers: the first listed wraps the plain function, and each later one wraps the result, so it sits further out.","concept":"Decorators in a view's decorators list are applied by hand as nested wrappers: the first listed wraps the plain function, and each later one wraps the result, so it sits further out.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["Decorator order is wrapping order: the last one applied is the outermost."]},{"id":"flaskadvanced-004","courseId":"flask-advanced","domain":"routing-and-requests","topic":"MethodView method dispatch","prompt":"A MethodView subclass defines get() and post() and nothing else. What decides which HTTP methods its route accepts?","hint":"Look at where the list of accepted methods gets set for a class-based view.","explain":"A class-based view can serve several HTTP verbs from one class, so the framework must know which verbs a route accepts. Consider where that knowledge could come from: the class, the route, or the request.","source":"https://flask.palletsprojects.com/en/stable/views/#method-dispatching-and-apis","interaction":{"type":"single"},"options":[{"id":"a","text":"The methods the class defines, which MethodView copies into View.methods automatically.","why":"MethodView sets View.methods from the methods defined on the class, so get and post give GET and POST."},{"id":"b","text":"Only the methods written by hand in a View.methods list.","whyNot":"MethodView sets View.methods automatically, so writing that list by hand is not required."},{"id":"c","text":"Uppercase method names, such as GET() and POST(), defined on the class.","whyNot":"Each HTTP method maps to a class method with the same lowercase name, so the names are lowercase."},{"id":"d","text":"The request body, since the body determines which class method runs.","whyNot":"The request method, not the body, selects the class method that handles the request."}],"answer":"a","explanation":"MethodView dispatches each request to the class method named after the lowercase HTTP method, and sets View.methods from the methods the class defines.","concept":"MethodView dispatches each request to the class method named after the lowercase HTTP method, and sets View.methods from the methods the class defines.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["Define get, post and the other verbs as methods; Flask derives the accepted verbs from them."]},{"id":"flaskadvanced-005","courseId":"flask-advanced","domain":"routing-and-requests","topic":"init_every_request flag","prompt":"With View.init_every_request set to False, one view instance serves every request, so per-request data should be kept in g rather than on self.","hint":"Think about how many objects exist across requests, and what that means for data stored on one of them.","explain":"A view class is normally built fresh for each request, so its attributes stay private. Changing when instances are built changes what requests share. Consider what reusing one object means for data written to it.","source":"https://flask.palletsprojects.com/en/stable/views/#view-lifetime-and-self","interaction":{"type":"true-false"},"options":[{"id":"a","text":"True","why":"One shared instance means self is common to all requests, so per-request data belongs in g."},{"id":"b","text":"False","whyNot":"With the flag off, a single instance is reused for each request, so writing to self during a request can affect other requests."}],"answer":"a","explanation":"With init_every_request set to False, one instance serves all requests, so self is shared and per-request data belongs in g.","concept":"With init_every_request set to False, one instance serves all requests, so self is shared and per-request data belongs in g.","rev":1,"updatedAt":"2026-10-24","kind":"scenario","takeaways":["A view class with init_every_request = False is shared across requests; keep request data in g."]}]