move note about next param, add example, other cleanup
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent 434c199 · Browse files · 1 file·+17 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+17 added−15 removed
- other:+17 added−15 removed
Provenance
- Commit
169a4e0- Tree
cce4110- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
434c199
1 file changed+17 added−15 removed
modified:docs/patterns/viewdecorators.rst+17 added-15 removed
14 unmodified lines15 Login Required Decorator16 ------------------------17 18 So let's implement such a decorator. A decorator is a function that19-returns a function. Pretty simple actually. The only thing you have to20-keep in mind when implementing something like this is to update the21-`__name__`, `__module__` and some other attributes of a function. This is22-often forgotten, but you don't have to do that by hand, there is a23-function for that that is used like a decorator (:func:`functools.wraps`).19+wraps and replaces another function. Since the original function is20+replaced, you need to remember to copy the original function's information21+to the new function. Use :func:`functools.wraps` to handle this for you.22 23 This example assumes that the login page is called ``'login'`` and that26-the current user is stored as `g.user` and ``None`` if there is no-one27-logged in::24+the current user is stored in ``g.user`` and is ``None`` if there is no-one25+logged in. ::26 27 from functools import wraps28 from flask import g, request, redirect, url_for29 4 unmodified lines34 return redirect(url_for('login', next=request.url))35 return f(*args, **kwargs)36 return decorated_function37 40-So how would you use that decorator now? Apply it as innermost decorator41-to a view function. When applying further decorators, always remember42-that the :meth:`~flask.Flask.route` decorator is the outermost.38+To use the decorator, apply it as innermost decorator to a view function.39+When applying further decorators, always remember40+that the :meth:`~flask.Flask.route` decorator is the outermost. ::41 44-While the ``next`` value may exist in ``request.args`` after a ``GET`` request for45-the login form, you'll have to pass it along when sending the ``POST`` request46-from the login form. You can do this with a hidden input tag and ``requests.values``47-or ``requests.form``.::48-42 @app.route('/secret_page')43 @login_required44 def secret_page():45 pass46 47+.. note::48+ The ``next`` value will exist in ``request.args`` after a ``GET`` request for49+ the login page. You'll have to pass it along when sending the ``POST`` request50+ from the login form. You can do this with a hidden input tag, then retrieve it51+ from ``request.form`` when logging the user in. ::52+53+ <input type="hidden" value="{{ request.args.get('next', '') }}"/>54+55+56 Caching Decorator57 -----------------58 59 Imagine you have a view function that does an expensive calculation and113 unmodified lines