Interview Q&A · questions with model answers
Flask Interview Questions
Ordered by how often they're asked. Most-asked answers are free to read in full; members see every answer and can rate their confidence on each.
-
Most asked Explain Flask's application context and request context.
The request context makes request and session available during a request; the application context makes current_app and g available. Both are pushed automatically per request and are stored in…
-
Most asked How does routing work in Flask, including variable rules and HTTP methods?
@app.route('/path/converter:name', methods=[...]) (or @app.get/@app.post shortcuts) maps URLs to view functions; converters like int, float, path, uuid and string parse and validate URL segments.
-
Most asked How do Flask sessions work and what are their security implications?
By default Flask stores session data client-side in a cookie signed with SECRET_KEY (via ItsDangerous). Users can read the contents (it's only signed, not encrypted) but cannot modify them without…
-
Most asked How does Jinja2 templating work in Flask, including inheritance and autoescaping?
render_template('page.html', context) renders Jinja2 templates from the templates/ folder. Templates support inheritance ({% extends %}, {% block %}), includes, macros and filters, and autoescaping…
-
Most asked How do you handle errors in Flask, including JSON APIs?
Register handlers with @app.errorhandler(code_or_exception) (or bp.app_errorhandler) and raise abort(code) or custom exceptions. For APIs, return JSON for HTTPExceptions by handling them generically.
-
Most asked How do Flask-WTF forms and CSRF protection work?
Flask-WTF integrates WTForms: define a FlaskForm with fields and validators, call form.validate_on_submit() in the view, and render {{ form.hidden_tag() }} to include the CSRF token, which is…
-
Most asked Walk through the lifecycle of a request in Flask.
The WSGI server calls app.wsgi_app; Flask creates a request context (which pushes an app context), matches the URL, runs before_request hooks, calls the view, converts the return value to a…
-
How do database migrations work with Flask-Migrate?
Flask-Migrate wraps Alembic: flask db init creates the migrations folder, flask db migrate -m 'msg' autogenerates a migration from model changes, and flask db upgrade applies it.
-
Does Flask support async views? What are the limitations?
Since Flask 2.0, view functions can be async def (install flask[async]), but Flask remains a WSGI framework: each async view runs in its own event loop inside a worker thread, so you don't get the…
-
How do redirects, `url_for` and flash messages work together in Flask?
After a successful POST, call flash('Saved!') to store a one-time message in the session, then return redirect(url_for('items.index')) (Post/Redirect/Get); the next page reads messages with…
-
How do you handle file uploads securely in Flask?
Read files from request.files, limit size with MAX_CONTENT_LENGTH, validate type by content (not just extension), sanitize names with werkzeug.utils.secure_filename or generate your own, and store…
-
How should transactions be handled in Flask views?
Treat each request as a unit of work: make changes on db.session, commit once at the end of successful processing, and roll back on exceptions (Flask-SQLAlchemy removes the session at teardown).
-
How does logging work in Flask?
Flask uses Python's standard logging; app.logger is a logger named after the app. Configure logging (handlers, formatters, levels) with logging.config.dictConfig before creating the app, and add…
-
Is Flask thread-safe? How do `request` and `current_app` work across threads?
Yes for typical use: request, session, g and current_app are context-local proxies (backed by contextvars), so each thread or greenlet sees its own request. Your own module-level mutable state is…
-
How does Flask handle HEAD, OPTIONS and 405 Method Not Allowed?
Flask automatically adds HEAD for GET routes and OPTIONS for all routes (with an Allow header); requesting an unregistered method on an existing URL returns 405 with the allowed methods.
-
Flask has no built-in dependency injection. How do you structure dependencies for testability?
Create services in the app factory (or attach them to app.extensions), access them through small accessor functions or current_app, and swap implementations in tests via configuration. Libraries…
-
Can you use Pydantic with Flask for request validation?
Yes. Validate request.get_json() with Model.model_validate(...) and return errors as 422, or use integrations like Flask-Pydantic or APIFlask/flask-openapi3 that add decorators and OpenAPI docs.
-
How are static files served in Flask, and how should they be served in production?
Flask serves files from the static/ folder at /static/... via url_for('static', filename=...). In production, serve them from Nginx or a CDN with cache headers, and use hashed filenames for cache…
-
How does Flask protect against XSS, and how can it still happen?
Jinja2 autoescaping escapes variables in .html templates. XSS still happens via |safe/Markup on user content, unescaped JavaScript contexts, render_template_string with user input (also SSTI), or…
-
How do you scale a Flask application horizontally?
Keep app servers stateless (sessions in signed cookies or Redis, files in object storage), run multiple Gunicorn instances behind a load balancer, scale the database separately (replicas, pooling),…
-
Does Flask support ETags and conditional requests?
Yes. Werkzeug responses support add_etag() and make_conditional(request), which return 304 when If-None-Match matches, and send_file sets ETags and Last-Modified automatically.
-
How does subdomain routing work in Flask?
Set SERVER_NAME (e.g., example.com) and declare routes or blueprints with subdomain='<tenant>' (or a fixed subdomain like api), and Flask matches the host to pass the subdomain to views.
-
How do type hints help in Flask projects?
Flask itself is typed, so annotating views, services and models lets mypy/pyright catch bugs, improves editor completion, and documents return types like ResponseReturnValue.