Skip to content
Myr

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

1 file+17 added−15 removed

modified:docs/patterns/viewdecorators.rst+17 added-15 removed
14 unmodified lines
15Login Required Decorator16------------------------1718So let's implement such a decorator. A decorator is a function that19returns a function. Pretty simple actually. The only thing you have to20keep in mind when implementing something like this is to update the21`__name__`, `__module__` and some other attributes of a function. This is22often forgotten, but you don't have to do that by hand, there is a23function for that that is used like a decorator (:func:`functools.wraps`).19wraps and replaces another function. Since the original function is20replaced, you need to remember to copy the original function's information21to the new function. Use :func:`functools.wraps` to handle this for you.2223This example assumes that the login page is called ``'login'`` and that26the current user is stored as `g.user` and ``None`` if there is no-one27logged in::24the current user is stored in ``g.user`` and is ``None`` if there is no-one25logged in. ::2627 from functools import wraps28 from flask import g, request, redirect, url_for29
4 unmodified lines
34 return redirect(url_for('login', next=request.url))35 return f(*args, **kwargs)36 return decorated_function3740So how would you use that decorator now? Apply it as innermost decorator41to a view function. When applying further decorators, always remember42that the :meth:`~flask.Flask.route` decorator is the outermost.38To use the decorator, apply it as innermost decorator to a view function.39When applying further decorators, always remember40that the :meth:`~flask.Flask.route` decorator is the outermost. ::4144While the ``next`` value may exist in ``request.args`` after a ``GET`` request for45the login form, you'll have to pass it along when sending the ``POST`` request46from the login form. You can do this with a hidden input tag and ``requests.values``47or ``requests.form``.::4842 @app.route('/secret_page')43 @login_required44 def secret_page():45 pass4647.. 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. ::5253 <input type="hidden" value="{{ request.args.get('next', '') }}"/>545556Caching Decorator57-----------------5859Imagine you have a view function that does an expensive calculation and
113 unmodified lines