question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mounts without a name strips the path arg from calls to child routes in url_path_for

See original GitHub issue

I’m using an unnamed Mount which contains a mount to a StaticFiles instance and when using “url_for” it is unable to find the matching file. After a bit of digging it seems to be directly related to url_for_path in Mount clearing the path arg whenever the mount does not have a name set.

https://github.com/encode/starlette/blob/master/starlette/routing.py#L337

Calling url_path_for in the child router works as expected. I’m unclear as to why you’d want to strip this arg just because the mount is not named.

app.mount('/stuff', app=Router(routes=[
    Mount('/static', app=StaticFiles(directory='static'), name='static'),
]))
In [6]: app.url_path_for('static', path='css/bootstap.min.css')
---------------------------------------------------------------------------
NoMatchFound
...
In [7]: app.routes[0].routes[0].url_path_for('static', path='css/bootstap.min.css')
Out[7]: '/static/css/bootstap.min.css'

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
tomchristiecommented, Nov 1, 2019

Thanks for raising this. There’s a few seperate issues here:

  • url_for was broken for the “Mount within Mount” case. Resolved in #701.
  • The docs are poor.
  • The raised exceptions are not helpful.

I’m going to spend some more comprehensive time on this next week.

I’m also planning to start switching our docs out to use the routing table style, instead of the decorator style eg…

routes = [
    Route('/', homepage),
    Mount('/static', app=StaticFiles(directory='static'), name='static'),
]

app = Starlette(routes=routes)

I think it’s a whole bunch cleaner & works better as projects grow.

That won’t mean getting rid of the decorator and add_route APIs, but it will mean changing the docs so that we’re pushing for that particular style, and sidelining the alternatives.

0reactions
rmorisoncommented, Oct 27, 2019

+1, spent 2h debugging before I thought to check repo:issues. Documentation is also lacking on how names compose in nested routing for url_for. This thread has more detail than the docs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue router taking me to parent route mistakenly - Stack Overflow
It seems there are conflicts on your root routes (every path with "/" in front). You could check the vue-router documentation for nested ......
Read more >
Ultimate React Router v6 Guide
React Router is by far the most popular routing library in React and this article goes in depth on everything you need to...
Read more >
Router tutorial: tour of heroes - Angular
In this tutorial, you build upon a basic router configuration to explore features such as child routes, route parameters, lazy load NgModules, guard...
Read more >
Programmatic Navigation - Vue Router
Aside from using <router-link> to create anchor tags for declarative ... the name of the route or manually specify the whole path with...
Read more >
React Router v6 Preview
Relative <Route path> and <Link to>; Nested Routes and Layouts ... React Router contains many modules and hooks that you may not even...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found