Browse Source

Documented non-decorator uses of registation.

pull/1638/head
Armin Ronacher 15 years ago
parent
commit
7d6b8e9ab3
  1. 36
      flask.py

36
flask.py

@ -288,6 +288,27 @@ class Flask(object):
if session is not None: if session is not None:
session.save_cookie(response, self.session_cookie_name) session.save_cookie(response, self.session_cookie_name)
def add_url_rule(self, endpoint, **options):
"""Connects a URL rule. Works exactly like the :meth:`route`
decorator but does not register the view function for the endpoint.
Basically this example::
@app.route('/')
def index():
pass
Is equivalent to the following:
def index():
pass
app.add_url_rule('index', '/')
app.view_functions['index'] = index
"""
options['endpoint'] = f.__name__
options.setdefault('methods', ('GET',))
self.url_map.add(Rule(rule, **options))
def route(self, rule, **options): def route(self, rule, **options):
"""A decorator that is used to register a view function for a """A decorator that is used to register a view function for a
given URL rule. Example:: given URL rule. Example::
@ -351,11 +372,8 @@ class Flask(object):
setting for this rule. See above. setting for this rule. See above.
""" """
def decorator(f): def decorator(f):
if 'endpoint' not in options: self.add_url_rule(f.__name__, **options)
options['endpoint'] = f.__name__ self.view_functions[f.__name__] = f
options.setdefault('methods', ('GET',))
self.url_map.add(Rule(rule, **options))
self.view_functions[options['endpoint']] = f
return f return f
return decorator return decorator
@ -366,6 +384,14 @@ class Flask(object):
@app.errorhandler(404) @app.errorhandler(404)
def page_not_found(): def page_not_found():
return 'This page does not exist', 404 return 'This page does not exist', 404
You can also register a function as error handler without using
the :meth:`errorhandler` decorator. The following example is
equivalent to the one above::
def page_not_found():
return 'This page does not exist', 404
app.error_handlers[404] = page_not_found
""" """
def decorator(f): def decorator(f):
self.error_handlers[code] = f self.error_handlers[code] = f

Loading…
Cancel
Save