Skip to content

Reference

This section is generated from the source code with mkdocstrings. It collects the package entry point and each module in one place so students can see both package-level and module-level documentation together.

Application factory for the Flask tutorial project.

create_app(test_config=None)

Create and configure the Flask application.

The factory loads default settings from :mod:flaskr.settings, applies any environment-specific overrides, and then wires up the database session and blueprints used by the tutorial app.

Parameters:

Name Type Description Default
test_config Mapping[str, Any] | None

Optional configuration values that override the default settings. This is primarily used by tests.

None

Returns:

Type Description
Flask

The configured Flask application instance.

Examples:

>>> app = create_app({"TESTING": True, "SECRET_KEY": "test"})
>>> app.config["TESTING"]
True

auth

Authentication views and helpers for the tutorial application.

register()

Register a new user account.

On GET requests this view renders the registration form. On POST requests it validates the submitted credentials, creates the user, and redirects to the login page when registration succeeds.

Returns:

Type Description
ResponseReturnValue

The rendered registration page or a redirect to the login page.

login()

Authenticate an existing user and start a session.

Returns:

Type Description
ResponseReturnValue

The rendered login form or a redirect to the blog index after a

ResponseReturnValue

successful login.

load_logged_in_user()

Load the current user from the session into flask.g.

This runs before every request so templates and view functions can access g.user without repeating the database lookup themselves.

login_required(view)

Require an authenticated user before executing a view function.

Parameters:

Name Type Description Default
view Callable[P, ResponseReturnValue]

View function to protect.

required

Returns:

Type Description
Callable[P, ResponseReturnValue]

A wrapped view that redirects anonymous visitors to the login page.

Examples:

>>> @bp.route("/drafts")
... @login_required
... def drafts():
...     return "Only logged-in users can see this page."
Note

Place @login_required below @bp.route(...) so Flask registers the protected view function.

logout()

Clear the active session and return to the home page.

Returns:

Type Description
ResponseReturnValue

A redirect to the blog index.

blog

Blog views for listing, creating, and editing posts.

index()

Show the blog home page with the newest posts first.

Returns:

Type Description
ResponseReturnValue

The rendered index page.

create()

Create a new post for the logged-in user.

Returns:

Type Description
ResponseReturnValue

The rendered creation form or a redirect to the index page after the

ResponseReturnValue

post is saved.

get_post(id)

Return a post and ensure the current user is allowed to edit it.

Parameters:

Name Type Description Default
id int

Identifier of the post to retrieve.

required

Returns:

Type Description
Post

The requested post.

Raises:

Type Description
NotFound

If no post exists with the given id.

Forbidden

If the current user is not the author of the post.

update(id)

Edit an existing post owned by the current user.

Parameters:

Name Type Description Default
id int

Identifier of the post to update.

required

Returns:

Type Description
ResponseReturnValue

The rendered update form or a redirect to the index page after saving.

delete(id)

Delete a post owned by the current user.

Parameters:

Name Type Description Default
id int

Identifier of the post to delete.

required

Returns:

Type Description
ResponseReturnValue

A redirect to the blog index.

db

Database utilities for configuring and accessing SQLAlchemy sessions.

create_db_session(database_url=None)

Create the scoped database session used by the application.

Parameters:

Name Type Description Default
database_url str | None

Database connection URL. When omitted, the local SQLite tutorial database is used.

None

Returns:

Type Description
scoped_session[Session]

A tuple containing the scoped session registry and a callback that

Callable[[BaseException | None], None]

removes the active session at the end of a request.

init_db(database_url=DEFAULT_DATABASE_URL)

Recreate the database schema from the SQLAlchemy models.

Parameters:

Name Type Description Default
database_url str

Database connection URL to initialize.

DEFAULT_DATABASE_URL
Warning

This function drops existing tables before recreating them. Use it only for local development, tests, or other disposable databases.

get_db_session()

Return the request-scoped database session.

Returns:

Type Description
scoped_session[Session]

The SQLAlchemy scoped session stored on the current Flask app.

Raises:

Type Description
RuntimeError

If called outside an active Flask application context.

models

SQLAlchemy ORM models used by the tutorial blog application.

Base

Bases: DeclarativeBase

Base class shared by all ORM models in the project.

User

Bases: Base

Registered user account.

Users can author blog posts and authenticate with a stored password hash.

Post

Bases: Base

Blog post written by a :class:User.

Each post stores a title, body text, creation timestamp, and the author who wrote it.

settings

Default configuration values for the tutorial application.

These settings keep the example project runnable out of the box while still being easy to override in tests, local environment files, or deployment configuration.