Skip to main content

Inspecting Blueprints and Endpoints

To identify which view or blueprint is handling a request in Flask, use the properties provided by the request object (an instance of flask.wrappers.Request). These properties allow you to conditionally apply logic in before_request handlers, context processors, or error handlers based on the routing context.

Identifying the Current View

The request.endpoint property returns the internal name Flask uses for the view function that matched the request URL. If the view is part of a blueprint, the endpoint name is prefixed with the blueprint's name.

from flask import Flask, request, Blueprint

app = Flask(__name__)
bp = Blueprint("auth", __name__)

@app.route("/")
def index():
# Returns "index"
return request.endpoint

@bp.route("/login")
def login():
# Returns "auth.login"
return request.endpoint

app.register_blueprint(bp, url_prefix="/auth")

If you manually specify an endpoint name in the route decorator, request.endpoint will return that name instead of the function name:

@bp.route("/logout", endpoint="signout")
def logout():
# Returns "auth.signout"
return request.endpoint

Determining Blueprint Context

To check if the current request is being handled by a specific blueprint, use request.blueprint. This returns the registered name of the blueprint or None if the request was matched to an application-level route.

@app.before_request
def check_auth_context():
if request.blueprint == "auth":
# Logic specific to the 'auth' blueprint
pass

Note that request.blueprint returns the name the blueprint was registered with, which may differ from the name used when the Blueprint object was created if it was renamed during registration:

# Created with name "bp"
bp = Blueprint("bp", __name__)

# Registered with name "alt"
app.register_blueprint(bp, name="alt")

# Inside a view in this blueprint, request.blueprint returns "alt"

Handling Nested Blueprints

For applications using nested blueprints, request.blueprints returns a list of blueprint names from the current leaf blueprint up through its parents.

parent = Blueprint("parent", __name__)
child = Blueprint("child", __name__)

@child.route("/info")
def info():
# If registered as parent.child, returns ["parent.child", "parent"]
return str(request.blueprints)

parent.register_blueprint(child, url_prefix="/child")
app.register_blueprint(parent, url_prefix="/parent")

This is particularly useful for checking if a request falls under a broad category of functionality (e.g., checking if any parent blueprint is "admin").

Inspecting URL Rules and Arguments

For deeper inspection of the matched route, use request.url_rule. This is the actual werkzeug.routing.Rule object, which contains metadata like the allowed HTTP methods.

@app.after_request
def log_methods(response):
if request.url_rule:
# Access allowed methods for this rule
methods = request.url_rule.methods
print(f"Endpoint {request.endpoint} allows: {methods}")
return response

You can also access request.view_args, which contains the dictionary of arguments extracted from the URL (e.g., variable parts like <id>).

Troubleshooting

  • None Values: request.endpoint, request.blueprint, and request.url_rule will be None if URL matching has not been performed yet (e.g., in some early-stage middlewares) or if the request failed to match any route (resulting in a 404).
  • Routing Exceptions: If matching fails, request.routing_exception will contain the exception (usually a werkzeug.exceptions.NotFound) that will be raised.
  • Dot Notation: Blueprint names in request.endpoint are separated by dots. Avoid manual string parsing of these names; use request.blueprint or request.blueprints instead.