Navigation

Python has a really interesting feature called function decorators. This
allows some really neat things for web applications. Because each view in
Flask is a function, decorators can be used to inject additional
functionality to one or more functions. The route()
decorator is the one you probably used already. But there are use cases
for implementing your own decorator. For instance, imagine you have a
view that should only be used by people that are logged in. If a user
goes to the site and is not logged in, they should be redirected to the
login page. This is a good example of a use case where a decorator is an
excellent solution.

So let’s implement such a decorator. A decorator is a function that
wraps and replaces another function. Since the original function is
replaced, you need to remember to copy the original function’s information
to the new function. Use functools.wraps() to handle this for you.

This example assumes that the login page is called 'login' and that
the current user is stored in g.user and is None if there is no-one
logged in.

To use the decorator, apply it as innermost decorator to a view function.
When applying further decorators, always remember
that the route() decorator is the outermost.

@app.route('/secret_page')@login_requireddefsecret_page():pass

Note

The next value will exist in request.args after a GET request for
the login page. You’ll have to pass it along when sending the POST request
from the login form. You can do this with a hidden input tag, then retrieve it
from request.form when logging the user in.

Imagine you have a view function that does an expensive calculation and
because of that you would like to cache the generated results for a
certain amount of time. A decorator would be nice for that. We’re
assuming you have set up a cache like mentioned in Caching.

Here is an example cache function. It generates the cache key from a
specific prefix (actually a format string) and the current path of the
request. Notice that we are using a function that first creates the
decorator that then decorates the function. Sounds awful? Unfortunately
it is a little bit more complex, but the code should still be
straightforward to read.

The decorated function will then work as follows

get the unique cache key for the current request base on the current
path.

get the value for that key from the cache. If the cache returned
something we will return that value.

otherwise the original function is called and the return value is
stored in the cache for the timeout provided (by default 5 minutes).

A common pattern invented by the TurboGears guys a while back is a
templating decorator. The idea of that decorator is that you return a
dictionary with the values passed to the template from the view function
and the template is automatically rendered. With that, the following
three examples do exactly the same:

As you can see, if no template name is provided it will use the endpoint
of the URL map with dots converted to slashes + '.html'. Otherwise
the provided template name is used. When the decorated function returns,
the dictionary returned is passed to the template rendering function. If
None is returned, an empty dictionary is assumed, if something else than
a dictionary is returned we return it from the function unchanged. That
way you can still use the redirect function or return simple strings.