Consider a Flask app using Flask-Login. What happens when a user logs in successfully?
Think about how Flask-Login keeps track of the logged-in user between requests.
Flask-Login stores the user ID in the session cookie. On each request, it loads the user from the database using this ID. It does not store the full user object or credentials in the session.
Which code snippet correctly initializes Flask-Migrate in a Flask app?
from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db' db = SQLAlchemy(app) # Initialize Flask-Migrate here
Check the order of arguments required by Flask-Migrate's constructor.
Flask-Migrate requires the Flask app instance first, then the SQLAlchemy database instance.
Given this Flask-Mail setup, emails are not sent and no error is shown. What is the likely cause?
from flask import Flask from flask_mail import Mail, Message app = Flask(__name__) app.config.update( MAIL_SERVER='smtp.example.com', MAIL_PORT=587, MAIL_USE_TLS=True, MAIL_USERNAME='user@example.com', MAIL_PASSWORD='password' ) mail = Mail(app) @app.route('/send') def send_email(): msg = Message('Hello', sender='user@example.com', recipients=['friend@example.com']) msg.body = 'This is a test email.' mail.send(msg) return 'Email sent!'
Consider Flask's application context when sending emails outside request handling.
Flask-Mail requires an application context to send emails properly. Without it, sending may fail silently.
Consider this Flask app using Flask-Caching with SimpleCache. What will be printed on the second request to '/'?
from flask import Flask from flask_caching import Cache app = Flask(__name__) app.config['CACHE_TYPE'] = 'SimpleCache' cache = Cache(app) @app.route('/') @cache.cached(timeout=60) def index(): print('Function called') return 'Hello World' # First request: prints 'Function called' and returns 'Hello World' # Second request within 60 seconds: ?
Think about what caching does to function calls.
The cached function is not called again within the timeout, so 'Function called' is not printed on the second request.
Which of the following best explains why Flask-Babel is preferred for adding localization support in Flask apps?
Consider how localization libraries handle language selection and formatting.
Flask-Babel works with Flask's request context to choose the right language and provides tools for translations and formatting dates and times.