如何使用 Pyramid 转义 url 内的子路径
我需要在 Pyramid 生成的路由中传递子路径作为参数。我尝试使用 urllib.encode 和 urllib.quote 但无论如何都会收到“资源无法找到错误”。
路线生成:
mypath='a/b/c'
new_route = route_url('new_model_route', self.request, subpath=urllib.encode(mypath))
我的路线:
config.add_route('new_model_route', '/model/new/{subpath}')
生成的网址(带有“资源未找到错误”)
http://127.0.0.1:6544/rnd2/model/new/a%2Fb%2Fc
我知道这与转义有关,因为网址 http://blah/model/new/a
有效。
I need to pass a subpath as a param in a route generated by Pyramid. I tried using urllib.encode and urllib.quote but get a "resource could not be found error" either way.
route generation:
mypath='a/b/c'
new_route = route_url('new_model_route', self.request, subpath=urllib.encode(mypath))
my route:
config.add_route('new_model_route', '/model/new/{subpath}')
generated url (with "resource not found error")
http://127.0.0.1:6544/rnd2/model/new/a%2Fb%2Fc
I know it has to do with the escaping because the url http://blah/model/new/a
works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
路线模式
/model/new/{subpath}
永远不会匹配/model/new/a/b/c
所以我不明白为什么你能够为该模式生成一个 URL?不过,可以将 url 的其余部分作为元素传递。
另一方面,你还有另一种选择。您可以创建一个新的路由来匹配这些网址,例如:
如果由于某种原因您实际上不想匹配这些网址,您可以将
static=True
添加到add_route 调用,这意味着您仅使用
路由名称
来生成 url,而不是在传入请求中匹配它们。subpath
和traverse
恰好很特殊(这一切都已记录),但如果您执意使用“a/b/c”,您可以在路由模式中使用其他内容:哦,既然我很高兴,你可以使用原始方法和一个更简单的网址,你的系统中可能已经有这个方法。
The route pattern
/model/new/{subpath}
would never match/model/new/a/b/c
so I don't understand why you would be able to generate a URL for that pattern?It is possible to pass the rest of the url as elements, however.
On the other hand, you have another option. You can create a new route to match these urls, such as:
If you didn't actually want to match these URLs for some reason, you can add
static=True
to theadd_route
call, which means that you are only using theroute name
to generate urls but not to match them on incoming requests.subpath
andtraverse
happen to be special (this is all documented) but if you were hellbent on using 'a/b/c' you could use something else in the route pattern:Oh and since I'm on a roll, you can use the original approach with a simpler url which you may already have in your system.