* Demonstrate how to add multiple urls to the same function endpoint * Removed text as per untitaker, fixed spacing to be pep-8 compliant
This commit is contained in:
parent
b8aca21a39
commit
9f2b3d815e
1 changed files with 10 additions and 5 deletions
|
|
@ -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
|
You can further optimize this in terms of amount of keystrokes needed to
|
||||||
write this by having a function that calls into
|
write this by having a function that calls into
|
||||||
:meth:`~flask.Flask.add_url_rule` by prefixing a string with the project
|
: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)
|
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')
|
# add a single route to the index view
|
||||||
url('/user/<username>', 'views.user')
|
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
|
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
|
to be in a file that is imported upfront to work properly on the first
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue