forked from orbit-oss/flask
Documented view function options
This commit is contained in:
parent
3b31df81ae
commit
76796c326d
2 changed files with 42 additions and 0 deletions
38
docs/api.rst
38
docs/api.rst
|
|
@ -451,3 +451,41 @@ Class Based Views
|
||||||
|
|
||||||
.. autoclass:: flask.views.MethodView
|
.. autoclass:: flask.views.MethodView
|
||||||
:members:
|
: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)
|
||||||
|
|
|
||||||
|
|
@ -678,6 +678,10 @@ class Flask(_PackageBoundObject):
|
||||||
|
|
||||||
app.view_functions['index'] = index
|
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
|
.. versionchanged:: 0.2
|
||||||
`view_func` parameter added.
|
`view_func` parameter added.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue