diff --git a/docs/api.rst b/docs/api.rst index b5254996..94a38ad5 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -451,3 +451,41 @@ Class Based Views .. autoclass:: flask.views.MethodView :members: + +.. _view-func-options: + +View Function Options +--------------------- + +For internal usage the view functions can have some attributes attached to +customize behavior the view function would normally not have control over. +The following attributes can be provided optionally to either override +some defaults to :meth:`~flask.Flask.add_url_rule` or general behavior: + +- `__name__`: The name of a function is by default used as endpoint. If + endpoint is provided explicitly this value is used. Additionally this + will be prefixed with the name of the blueprint by default which + cannot be customized from the function itself. + +- `methods`: If methods are not provided when the URL rule is added, + Flask will look on the view function object itself is an `methods` + attribute exists. If it does, it will pull the information for the + methods from there. + +- `provide_automatic_options`: if this attribute is set Flask will + either force enable or disable the automatic implementation of the + HTTP `OPTIONS` response. This can be useful when working with + decorators that want to customize the `OPTIONS` response on a per-view + basis. + +Full example:: + + def index(): + if request.method == 'OPTIONS': + # custom options handling here + ... + return 'Hello World!' + index.provide_automatic_options = False + index.methods = ['GET', 'OPTIONS'] + + app.add_url_rule('/', index) diff --git a/flask/app.py b/flask/app.py index db9d2af0..8ca74e30 100644 --- a/flask/app.py +++ b/flask/app.py @@ -678,6 +678,10 @@ class Flask(_PackageBoundObject): app.view_functions['index'] = index + If a view function is provided some defaults can be specified directly + on the view function. For more information refer to + :ref:`view-func-options`. + .. versionchanged:: 0.2 `view_func` parameter added.