Allow passing None to url_for() to get the url_prefix

This commit is contained in:
Kovid Goyal 2015-10-11 12:47:44 +05:30
parent c502d64c0e
commit 65c6d6d86c
2 changed files with 3 additions and 0 deletions

View File

@ -290,5 +290,7 @@ class Router(object):
return ans return ans
def url_for(self, route, **kwargs): def url_for(self, route, **kwargs):
if route is None:
return self.url_prefix or '/'
route = getattr(route, 'route_key', route) route = getattr(route, 'route_key', route)
return self.url_prefix + self.routes[route].url_for(**kwargs) return self.url_prefix + self.routes[route].url_for(**kwargs)

View File

@ -97,3 +97,4 @@ class TestRouter(BaseTest):
self.ae(ep, soak_opt), self.ae(args, ['a/b']) self.ae(ep, soak_opt), self.ae(args, ['a/b'])
self.ae(router.url_for('/needs quoting', x='a/b c'), '/needs quoting/a%2Fb%20c') self.ae(router.url_for('/needs quoting', x='a/b c'), '/needs quoting/a%2Fb%20c')
self.ae(router.url_for(None), '/')