forked from orbit-oss/flask
Merge pull request #2371 from davidism/register_blueprint-doc
document `Flask.register_blueprint` arguments
This commit is contained in:
commit
2c97ed985e
2 changed files with 39 additions and 14 deletions
30
flask/app.py
30
flask/app.py
|
|
@ -995,21 +995,39 @@ class Flask(_PackageBoundObject):
|
||||||
|
|
||||||
@setupmethod
|
@setupmethod
|
||||||
def register_blueprint(self, blueprint, **options):
|
def register_blueprint(self, blueprint, **options):
|
||||||
"""Registers a blueprint on the application.
|
"""Register a :class:`~flask.Blueprint` on the application. Keyword
|
||||||
|
arguments passed to this method will override the defaults set on the
|
||||||
|
blueprint.
|
||||||
|
|
||||||
|
Calls the blueprint's :meth:`~flask.Blueprint.register` method after
|
||||||
|
recording the blueprint in the application's :attr:`blueprints`.
|
||||||
|
|
||||||
|
:param blueprint: The blueprint to register.
|
||||||
|
:param url_prefix: Blueprint routes will be prefixed with this.
|
||||||
|
:param subdomain: Blueprint routes will match on this subdomain.
|
||||||
|
:param url_defaults: Blueprint routes will use these default values for
|
||||||
|
view arguments.
|
||||||
|
:param options: Additional keyword arguments are passed to
|
||||||
|
:class:`~flask.blueprints.BlueprintSetupState`. They can be
|
||||||
|
accessed in :meth:`~flask.Blueprint.record` callbacks.
|
||||||
|
|
||||||
.. versionadded:: 0.7
|
.. versionadded:: 0.7
|
||||||
"""
|
"""
|
||||||
first_registration = False
|
first_registration = False
|
||||||
|
|
||||||
if blueprint.name in self.blueprints:
|
if blueprint.name in self.blueprints:
|
||||||
assert self.blueprints[blueprint.name] is blueprint, \
|
assert self.blueprints[blueprint.name] is blueprint, (
|
||||||
'A blueprint\'s name collision occurred between %r and ' \
|
'A name collision occurred between blueprints %r and %r. Both'
|
||||||
'%r. Both share the same name "%s". Blueprints that ' \
|
' share the same name "%s". Blueprints that are created on the'
|
||||||
'are created on the fly need unique names.' % \
|
' fly need unique names.' % (
|
||||||
(blueprint, self.blueprints[blueprint.name], blueprint.name)
|
blueprint, self.blueprints[blueprint.name], blueprint.name
|
||||||
|
)
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
self.blueprints[blueprint.name] = blueprint
|
self.blueprints[blueprint.name] = blueprint
|
||||||
self._blueprint_order.append(blueprint)
|
self._blueprint_order.append(blueprint)
|
||||||
first_registration = True
|
first_registration = True
|
||||||
|
|
||||||
blueprint.register(self, options, first_registration)
|
blueprint.register(self, options, first_registration)
|
||||||
|
|
||||||
def iter_blueprints(self):
|
def iter_blueprints(self):
|
||||||
|
|
|
||||||
|
|
@ -159,18 +159,25 @@ class Blueprint(_PackageBoundObject):
|
||||||
return BlueprintSetupState(self, app, options, first_registration)
|
return BlueprintSetupState(self, app, options, first_registration)
|
||||||
|
|
||||||
def register(self, app, options, first_registration=False):
|
def register(self, app, options, first_registration=False):
|
||||||
"""Called by :meth:`Flask.register_blueprint` to register a blueprint
|
"""Called by :meth:`Flask.register_blueprint` to register all views
|
||||||
on the application. This can be overridden to customize the register
|
and callbacks registered on the blueprint with the application. Creates
|
||||||
behavior. Keyword arguments from
|
a :class:`.BlueprintSetupState` and calls each :meth:`record` callback
|
||||||
:func:`~flask.Flask.register_blueprint` are directly forwarded to this
|
with it.
|
||||||
method in the `options` dictionary.
|
|
||||||
|
:param app: The application this blueprint is being registered with.
|
||||||
|
:param options: Keyword arguments forwarded from
|
||||||
|
:meth:`~Flask.register_blueprint`.
|
||||||
|
:param first_registration: Whether this is the first time this
|
||||||
|
blueprint has been registered on the application.
|
||||||
"""
|
"""
|
||||||
self._got_registered_once = True
|
self._got_registered_once = True
|
||||||
state = self.make_setup_state(app, options, first_registration)
|
state = self.make_setup_state(app, options, first_registration)
|
||||||
|
|
||||||
if self.has_static_folder:
|
if self.has_static_folder:
|
||||||
state.add_url_rule(self.static_url_path + '/<path:filename>',
|
state.add_url_rule(
|
||||||
view_func=self.send_static_file,
|
self.static_url_path + '/<path:filename>',
|
||||||
endpoint='static')
|
view_func=self.send_static_file, endpoint='static'
|
||||||
|
)
|
||||||
|
|
||||||
for deferred in self.deferred_functions:
|
for deferred in self.deferred_functions:
|
||||||
deferred(state)
|
deferred(state)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue