From 9f2b3d815ec39279bfb838221580a36378a0de20 Mon Sep 17 00:00:00 2001 From: dcfix Date: Thu, 16 Jun 2016 14:40:23 -0600 Subject: [PATCH] Demonstrate how to add multiple urls to the same function endpoint #981 (#1900) * Demonstrate how to add multiple urls to the same function endpoint * Removed text as per untitaker, fixed spacing to be pep-8 compliant --- docs/patterns/lazyloading.rst | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/docs/patterns/lazyloading.rst b/docs/patterns/lazyloading.rst index 8388102a..acb77f94 100644 --- a/docs/patterns/lazyloading.rst +++ b/docs/patterns/lazyloading.rst @@ -90,14 +90,19 @@ Then you can define your central place to combine the views like this:: You can further optimize this in terms of amount of keystrokes needed to write this by having a function that calls into :meth:`~flask.Flask.add_url_rule` by prefixing a string with the project -name and a dot, and by wrapping `view_func` in a `LazyView` as needed:: +name and a dot, and by wrapping `view_func` in a `LazyView` as needed. :: - def url(url_rule, import_name, **options): + def url(import_name, url_rules=[], **options): view = LazyView('yourapplication.' + import_name) - app.add_url_rule(url_rule, view_func=view, **options) + for url_rule in url_rules: + app.add_url_rule(url_rule, view_func=view, **options) - url('/', 'views.index') - url('/user/', 'views.user') + # add a single route to the index view + url('views.index', ['/']) + + # add two routes to a single function endpoint + url_rules = ['/user/','/user/'] + url('views.user', url_rules) One thing to keep in mind is that before and after request handlers have to be in a file that is imported upfront to work properly on the first