Python的 - 瓶默认路线可能吗? [英] Python - Flask Default Route possible?

查看:173
本文介绍了Python的 - 瓶默认路线可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Cherrypy中可以这样做:

  @ cherrypy.expose 
def default(self,url, *郊区,** kwarg):
通过

在Flask的网站上有一个关于烧瓶的全包式路线的代码片段。 你可以在这里找到



基本上装饰者的作品通过链接两个URL过滤器。在这个页面上的例子是:

$ $ p $ @ app.route('/',defaults = {'path':''} )
@ app.route('/< path:path>')
def catch_all(path):
return'你想要路径:%s'%path

哪一个会给你:

 %curl 127.0.0.1:5000#匹配第一条规则
你想要的路径:
%curl 127.0.0.1:5000/foo/bar#匹配第二条规则
你想要路径:foo / bar


In Cherrypy it's possible to do this:

@cherrypy.expose
def default(self, url, *suburl, **kwarg):
    pass

Is there a flask equivalent?

解决方案

There is a snippet on Flask's website about a 'catch-all' route for flask. You can find it here.

Basically the decorator works by chaining two URL filters. The example on the page is:

@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
    return 'You want path: %s' % path

Which would give you:

% curl 127.0.0.1:5000          # Matches the first rule
You want path:  
% curl 127.0.0.1:5000/foo/bar  # Matches the second rule
You want path: foo/bar

这篇关于Python的 - 瓶默认路线可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆