Skip to content
Myr

Demonstrate how to add multiple urls to the same function endpoint #981 (#1900)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Demonstrate how to add multiple urls to the same function endpoint * Removed text as per untitaker, fixed spacing to be pep-8 compliant

authored by dcfix · committed by Markus Unterwaditzer · parent b8aca21 · Browse files · 1 file+10 added−5 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+10 added−5 removed

  • other:+10 added−5 removed

1 file+10 added−5 removed

modified:docs/patterns/lazyloading.rst+10 added-5 removed
88 unmodified lines
8990You can further optimize this in terms of amount of keystrokes needed to91write this by having a function that calls into92:meth:`~flask.Flask.add_url_rule` by prefixing a string with the project93name and a dot, and by wrapping `view_func` in a `LazyView` as needed::93name and a dot, and by wrapping `view_func` in a `LazyView` as needed. ::9495 def url(url_rule, import_name, **options):95 def url(import_name, url_rules=[], **options):96 view = LazyView('yourapplication.' + import_name)97 for url_rule in url_rules:97 app.add_url_rule(url_rule, view_func=view, **options)98 app.add_url_rule(url_rule, view_func=view, **options)9999 url('/', 'views.index')100 url('/user/<username>', 'views.user')100 # add a single route to the index view101 url('views.index', ['/'])102103 # add two routes to a single function endpoint104 url_rules = ['/user/','/user/<username>']105 url('views.user', url_rules)106107One thing to keep in mind is that before and after request handlers have108to be in a file that is imported upfront to work properly on the first109request. The same goes for any kind of remaining decorator.