Make it easier to associate a rule, endpoint and view_function

This resolves http://github.com/mitsuhiko/flask/issues/issue/11
This commit is contained in:
unknown 2010-04-20 00:29:56 +08:00 committed by Armin Ronacher
parent 36f105c293
commit 3088a9371e
2 changed files with 33 additions and 3 deletions

View file

@ -481,9 +481,9 @@ class Flask(object):
"""
session.save_cookie(response, self.session_cookie_name)
def add_url_rule(self, rule, endpoint, **options):
def add_url_rule(self, rule, endpoint, view_func=None, **options):
"""Connects a URL rule. Works exactly like the :meth:`route`
decorator but does not register the view function for the endpoint.
decorator. If a view_func is provided it will be registered with the endpoint.
Basically this example::
@ -495,19 +495,25 @@ class Flask(object):
def index():
pass
app.add_url_rule('index', '/')
app.add_url_rule('/', 'index', index)
If the view_func is not provided you will need to connect the endpoint to a
view function like so:
app.view_functions['index'] = index
:param rule: the URL rule as string
:param endpoint: the endpoint for the registered URL rule. Flask
itself assumes the name of the view function as
endpoint
:param view_func: the function to call when servicing a request to the provided endpoint
:param options: the options to be forwarded to the underlying
:class:`~werkzeug.routing.Rule` object
"""
options['endpoint'] = endpoint
options.setdefault('methods', ('GET',))
self.url_map.add(Rule(rule, **options))
if view_func is not None:
self.view_functions[endpoint] = view_func
def route(self, rule, **options):
"""A decorator that is used to register a view function for a