Browse Source

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
pull/1917/head
dcfix 9 years ago committed by Markus Unterwaditzer
parent
commit
9f2b3d815e
  1. 13
      docs/patterns/lazyloading.rst

13
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)
for url_rule in url_rules:
app.add_url_rule(url_rule, view_func=view, **options)
url('/', 'views.index')
url('/user/<username>', '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/<username>']
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

Loading…
Cancel
Save