Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 129568f

Browse filesBrowse files
authored
Merge pull request #4971 from pallets/docs-blueprint-app-methods
point to app-scoped blueprint methods
2 parents d552726 + ab93222 commit 129568f
Copy full SHA for 129568f

File tree

Expand file treeCollapse file tree

2 files changed

+77
-33
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+77
-33
lines changed

‎src/flask/blueprints.py

Copy file name to clipboardExpand all lines: src/flask/blueprints.py
+38-32Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,11 @@ def add_url_rule(
478478
provide_automatic_options: t.Optional[bool] = None,
479479
**options: t.Any,
480480
) -> None:
481-
"""Like :meth:`Flask.add_url_rule` but for a blueprint. The endpoint for
482-
the :func:`url_for` function is prefixed with the name of the blueprint.
481+
"""Register a URL rule with the blueprint. See :meth:`.Flask.add_url_rule` for
482+
full documentation.
483+
484+
The URL rule is prefixed with the blueprint's URL prefix. The endpoint name,
485+
used with :func:`url_for`, is prefixed with the blueprint's name.
483486
"""
484487
if endpoint and "." in endpoint:
485488
raise ValueError("'endpoint' may not contain a dot '.' character.")
@@ -501,8 +504,8 @@ def add_url_rule(
501504
def app_template_filter(
502505
self, name: t.Optional[str] = None
503506
) -> t.Callable[[T_template_filter], T_template_filter]:
504-
"""Register a custom template filter, available application wide. Like
505-
:meth:`Flask.template_filter` but for a blueprint.
507+
"""Register a template filter, available in any template rendered by the
508+
application. Equivalent to :meth:`.Flask.template_filter`.
506509
507510
:param name: the optional name of the filter, otherwise the
508511
function name will be used.
@@ -518,9 +521,9 @@ def decorator(f: T_template_filter) -> T_template_filter:
518521
def add_app_template_filter(
519522
self, f: ft.TemplateFilterCallable, name: t.Optional[str] = None
520523
) -> None:
521-
"""Register a custom template filter, available application wide. Like
522-
:meth:`Flask.add_template_filter` but for a blueprint. Works exactly
523-
like the :meth:`app_template_filter` decorator.
524+
"""Register a template filter, available in any template rendered by the
525+
application. Works like the :meth:`app_template_filter` decorator. Equivalent to
526+
:meth:`.Flask.add_template_filter`.
524527
525528
:param name: the optional name of the filter, otherwise the
526529
function name will be used.
@@ -535,8 +538,8 @@ def register_template(state: BlueprintSetupState) -> None:
535538
def app_template_test(
536539
self, name: t.Optional[str] = None
537540
) -> t.Callable[[T_template_test], T_template_test]:
538-
"""Register a custom template test, available application wide. Like
539-
:meth:`Flask.template_test` but for a blueprint.
541+
"""Register a template test, available in any template rendered by the
542+
application. Equivalent to :meth:`.Flask.template_test`.
540543
541544
.. versionadded:: 0.10
542545
@@ -554,9 +557,9 @@ def decorator(f: T_template_test) -> T_template_test:
554557
def add_app_template_test(
555558
self, f: ft.TemplateTestCallable, name: t.Optional[str] = None
556559
) -> None:
557-
"""Register a custom template test, available application wide. Like
558-
:meth:`Flask.add_template_test` but for a blueprint. Works exactly
559-
like the :meth:`app_template_test` decorator.
560+
"""Register a template test, available in any template rendered by the
561+
application. Works like the :meth:`app_template_test` decorator. Equivalent to
562+
:meth:`.Flask.add_template_test`.
560563
561564
.. versionadded:: 0.10
562565
@@ -573,8 +576,8 @@ def register_template(state: BlueprintSetupState) -> None:
573576
def app_template_global(
574577
self, name: t.Optional[str] = None
575578
) -> t.Callable[[T_template_global], T_template_global]:
576-
"""Register a custom template global, available application wide. Like
577-
:meth:`Flask.template_global` but for a blueprint.
579+
"""Register a template global, available in any template rendered by the
580+
application. Equivalent to :meth:`.Flask.template_global`.
578581
579582
.. versionadded:: 0.10
580583
@@ -592,9 +595,9 @@ def decorator(f: T_template_global) -> T_template_global:
592595
def add_app_template_global(
593596
self, f: ft.TemplateGlobalCallable, name: t.Optional[str] = None
594597
) -> None:
595-
"""Register a custom template global, available application wide. Like
596-
:meth:`Flask.add_template_global` but for a blueprint. Works exactly
597-
like the :meth:`app_template_global` decorator.
598+
"""Register a template global, available in any template rendered by the
599+
application. Works like the :meth:`app_template_global` decorator. Equivalent to
600+
:meth:`.Flask.add_template_global`.
598601
599602
.. versionadded:: 0.10
600603
@@ -609,8 +612,8 @@ def register_template(state: BlueprintSetupState) -> None:
609612

610613
@setupmethod
611614
def before_app_request(self, f: T_before_request) -> T_before_request:
612-
"""Like :meth:`Flask.before_request`. Such a function is executed
613-
before each request, even if outside of a blueprint.
615+
"""Like :meth:`before_request`, but before every request, not only those handled
616+
by the blueprint. Equivalent to :meth:`.Flask.before_request`.
614617
"""
615618
self.record_once(
616619
lambda s: s.app.before_request_funcs.setdefault(None, []).append(f)
@@ -621,8 +624,8 @@ def before_app_request(self, f: T_before_request) -> T_before_request:
621624
def before_app_first_request(
622625
self, f: T_before_first_request
623626
) -> T_before_first_request:
624-
"""Like :meth:`Flask.before_first_request`. Such a function is
625-
executed before the first request to the application.
627+
"""Register a function to run before the first request to the application is
628+
handled by the worker. Equivalent to :meth:`.Flask.before_first_request`.
626629
627630
.. deprecated:: 2.2
628631
Will be removed in Flask 2.3. Run setup code when creating
@@ -642,8 +645,8 @@ def before_app_first_request(
642645

643646
@setupmethod
644647
def after_app_request(self, f: T_after_request) -> T_after_request:
645-
"""Like :meth:`Flask.after_request` but for a blueprint. Such a function
646-
is executed after each request, even if outside of the blueprint.
648+
"""Like :meth:`after_request`, but after every request, not only those handled
649+
by the blueprint. Equivalent to :meth:`.Flask.after_request`.
647650
"""
648651
self.record_once(
649652
lambda s: s.app.after_request_funcs.setdefault(None, []).append(f)
@@ -652,9 +655,8 @@ def after_app_request(self, f: T_after_request) -> T_after_request:
652655

653656
@setupmethod
654657
def teardown_app_request(self, f: T_teardown) -> T_teardown:
655-
"""Like :meth:`Flask.teardown_request` but for a blueprint. Such a
656-
function is executed when tearing down each request, even if outside of
657-
the blueprint.
658+
"""Like :meth:`teardown_request`, but after every request, not only those
659+
handled by the blueprint. Equivalent to :meth:`.Flask.teardown_request`.
658660
"""
659661
self.record_once(
660662
lambda s: s.app.teardown_request_funcs.setdefault(None, []).append(f)
@@ -665,8 +667,8 @@ def teardown_app_request(self, f: T_teardown) -> T_teardown:
665667
def app_context_processor(
666668
self, f: T_template_context_processor
667669
) -> T_template_context_processor:
668-
"""Like :meth:`Flask.context_processor` but for a blueprint. Such a
669-
function is executed each request, even if outside of the blueprint.
670+
"""Like :meth:`context_processor`, but for templates rendered by every view, not
671+
only by the blueprint. Equivalent to :meth:`.Flask.context_processor`.
670672
"""
671673
self.record_once(
672674
lambda s: s.app.template_context_processors.setdefault(None, []).append(f)
@@ -677,8 +679,8 @@ def app_context_processor(
677679
def app_errorhandler(
678680
self, code: t.Union[t.Type[Exception], int]
679681
) -> t.Callable[[T_error_handler], T_error_handler]:
680-
"""Like :meth:`Flask.errorhandler` but for a blueprint. This
681-
handler is used for all requests, even if outside of the blueprint.
682+
"""Like :meth:`errorhandler`, but for every request, not only those handled by
683+
the blueprint. Equivalent to :meth:`.Flask.errorhandler`.
682684
"""
683685

684686
def decorator(f: T_error_handler) -> T_error_handler:
@@ -691,15 +693,19 @@ def decorator(f: T_error_handler) -> T_error_handler:
691693
def app_url_value_preprocessor(
692694
self, f: T_url_value_preprocessor
693695
) -> T_url_value_preprocessor:
694-
"""Same as :meth:`url_value_preprocessor` but application wide."""
696+
"""Like :meth:`url_value_preprocessor`, but for every request, not only those
697+
handled by the blueprint. Equivalent to :meth:`.Flask.url_value_preprocessor`.
698+
"""
695699
self.record_once(
696700
lambda s: s.app.url_value_preprocessors.setdefault(None, []).append(f)
697701
)
698702
return f
699703

700704
@setupmethod
701705
def app_url_defaults(self, f: T_url_defaults) -> T_url_defaults:
702-
"""Same as :meth:`url_defaults` but application wide."""
706+
"""Like :meth:`url_defaults`, but for every request, not only those handled by
707+
the blueprint. Equivalent to :meth:`.Flask.url_defaults`.
708+
"""
703709
self.record_once(
704710
lambda s: s.app.url_default_functions.setdefault(None, []).append(f)
705711
)

‎src/flask/scaffold.py

Copy file name to clipboardExpand all lines: src/flask/scaffold.py
+39-1Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,11 @@ def load_user():
561561
a non-``None`` value, the value is handled as if it was the
562562
return value from the view, and further request handling is
563563
stopped.
564+
565+
This is available on both app and blueprint objects. When used on an app, this
566+
executes before every request. When used on a blueprint, this executes before
567+
every request that the blueprint handles. To register with a blueprint and
568+
execute before every request, use :meth:`.Blueprint.before_app_request`.
564569
"""
565570
self.before_request_funcs.setdefault(None, []).append(f)
566571
return f
@@ -577,6 +582,11 @@ def after_request(self, f: T_after_request) -> T_after_request:
577582
``after_request`` functions will not be called. Therefore, this
578583
should not be used for actions that must execute, such as to
579584
close resources. Use :meth:`teardown_request` for that.
585+
586+
This is available on both app and blueprint objects. When used on an app, this
587+
executes after every request. When used on a blueprint, this executes after
588+
every request that the blueprint handles. To register with a blueprint and
589+
execute after every request, use :meth:`.Blueprint.after_app_request`.
580590
"""
581591
self.after_request_funcs.setdefault(None, []).append(f)
582592
return f
@@ -606,6 +616,11 @@ def teardown_request(self, f: T_teardown) -> T_teardown:
606616
``try``/``except`` block and log any errors.
607617
608618
The return values of teardown functions are ignored.
619+
620+
This is available on both app and blueprint objects. When used on an app, this
621+
executes after every request. When used on a blueprint, this executes after
622+
every request that the blueprint handles. To register with a blueprint and
623+
execute after every request, use :meth:`.Blueprint.teardown_app_request`.
609624
"""
610625
self.teardown_request_funcs.setdefault(None, []).append(f)
611626
return f
@@ -615,7 +630,15 @@ def context_processor(
615630
self,
616631
f: T_template_context_processor,
617632
) -> T_template_context_processor:
618-
"""Registers a template context processor function."""
633+
"""Registers a template context processor function. These functions run before
634+
rendering a template. The keys of the returned dict are added as variables
635+
available in the template.
636+
637+
This is available on both app and blueprint objects. When used on an app, this
638+
is called for every rendered template. When used on a blueprint, this is called
639+
for templates rendered from the blueprint's views. To register with a blueprint
640+
and affect every template, use :meth:`.Blueprint.app_context_processor`.
641+
"""
619642
self.template_context_processors[None].append(f)
620643
return f
621644

@@ -635,6 +658,11 @@ def url_value_preprocessor(
635658
636659
The function is passed the endpoint name and values dict. The return
637660
value is ignored.
661+
662+
This is available on both app and blueprint objects. When used on an app, this
663+
is called for every request. When used on a blueprint, this is called for
664+
requests that the blueprint handles. To register with a blueprint and affect
665+
every request, use :meth:`.Blueprint.app_url_value_preprocessor`.
638666
"""
639667
self.url_value_preprocessors[None].append(f)
640668
return f
@@ -644,6 +672,11 @@ def url_defaults(self, f: T_url_defaults) -> T_url_defaults:
644672
"""Callback function for URL defaults for all view functions of the
645673
application. It's called with the endpoint and values and should
646674
update the values passed in place.
675+
676+
This is available on both app and blueprint objects. When used on an app, this
677+
is called for every request. When used on a blueprint, this is called for
678+
requests that the blueprint handles. To register with a blueprint and affect
679+
every request, use :meth:`.Blueprint.app_url_defaults`.
647680
"""
648681
self.url_default_functions[None].append(f)
649682
return f
@@ -667,6 +700,11 @@ def page_not_found(error):
667700
def special_exception_handler(error):
668701
return 'Database connection failed', 500
669702
703+
This is available on both app and blueprint objects. When used on an app, this
704+
can handle errors from every request. When used on a blueprint, this can handle
705+
errors from requests that the blueprint handles. To register with a blueprint
706+
and affect every request, use :meth:`.Blueprint.app_errorhandler`.
707+
670708
.. versionadded:: 0.7
671709
Use :meth:`register_error_handler` instead of modifying
672710
:attr:`error_handler_spec` directly, for application wide error

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.