【问题标题】:bottlepy route match any url with exceptionsBottlepy 路由匹配任何 url,但有例外
【发布时间】:2014-06-10 12:12:15
【问题描述】:

我正在为每个请求在瓶子上编写一个模板。所以 localhost 和 locahost/mypage 和 localhost/mypage/about 将调用相同的模板。我在这里检查并找到了一些关于匹配所有 url 的好例子:

from bottle import route, run, template, static_file
@route("/<url:re:.+>")
def hello(url):     
   return template('page_template', url=url)    

@route('/static/<filepath:path>', name='static')
def server_static(filepath):
    return static_file(filepath, root='static')

run()

我的问题是: 1)它与根不匹配。所以如果我输入“localhost”,它就不起作用。 2)由于有静态文件,我有另一个静态文件服务的路线。因此,如果我键入 localhost/static/page,它不会返回“hello world”。

我相信我需要修改正则表达式 (/<:re:.>) 来处理这两种情况。任何帮助将不胜感激,

@迈克尔

【问题讨论】:

  • 不太了解瓶子,但你需要一个“网址”吗? stackoverflow.com/a/8189597/792238
  • 感谢您指出。我已经编辑过了,
  • (FTR,每个帖子最好问一个问题。)

标签: python regex bottle


【解决方案1】:

好消息:您可以非常直接地“堆叠”路由。只需这样做:

@route("/")
@route("/<url:re:.+>")
def hello(url):     
   return template('page_template', url=url)    

这会将 root 视为与您现有的正则表达式路由相同。


至于重叠路由,根据the documentation,动态路由按照定义的顺序进行评估。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 2017-11-13
    • 2019-02-22
    • 1970-01-01
    • 2016-10-08
    相关资源
    最近更新 更多