Flask和其他Python框架中的多个URL段 [英] Multiple URL segment in Flask and other Python frameowrks

查看:271
本文介绍了Flask和其他Python框架中的多个URL段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读过瓶子和瓶子中的一个应用程序,通过两者的路由文档,这是非常清楚和可以理解的,但我正在努力找到一种方式来处理未知的,可能无限数量的网段。即:

 http://www.example.com/seg1/seg2/seg3/seg4/seg5 ..... 

我正在使用

  @ app.route(/< path:fullurl>) 

使用正则表达式去除不需要的字符并将fullurl字符串拆分成与段数相同长度的列表,但这看起来效率极低。



<大多数PHP框架似乎有一个方法来构建段变量名称的数组,无论数量如何,但Flask,Bottle或Django似乎都没有类似的选项,我似乎需要指定确切数量的段来捕获变量。一些PHP的CMS似乎收集的前9个部分立即作为变量,任何东西都作为一个完整的路径被传递,然后按照我上面提到的方式分解。



<我不理解的东西在URL路由工作的方式?字符串拆分方法真的效率低下还是最好的方法呢?或者,有没有办法在Flask中直接收集未知数量的段?



我是Python框架的新手,所以五年前的解释会有帮助,



非常感谢。

解决方案

烧瓶我自己,但从我迄今为止的工作,我敢肯定,这个想法是,你有很多小的路线/查看方法,而不是一个巨大的切换野兽。



例如,如果您有这样的网址:

  http://example.com/unit / 57 / 
http://example.com/unit/57/page/23/
http://example.com/unit/57/page/23/edit

您可以像这样设置路由:

  @ app.route('/ unit /< int:unit_number> /')
def display_unit(unit_number):
...

@app。路线('/ unit /< int:unit_number> / page /< int:page_number> /')
def display_page(unit_number,pag e_number):
...

@ app.route('/ unit /< int:unit_number> / page /< int:page_number> / edit')
def page_editor(unit_number,page_number):
...

这样做有助于在应用程序中保留某种结构,并依靠框架来路由东西,而不是抓取URL并自己完成所有的路由。然后,你也可以利用蓝图来处理不同的功能。



但是我承认,我正在努力想一个情况,你需要一个可能无限制的网址部分?


I'm building an application in both Bottle and Flask to see which I am more comfortable with as Django is too much 'batteries included'.

I have read through the routing documentation of both, which is very clear and understandable but I am struggling to find a way of dealing with an unknown, possibly unlimited number of URL segments. ie:

http://www.example.com/seg1/seg2/seg3/seg4/seg5.....

I was looking at using something like

@app.route(/< path:fullurl >)

using regex to remove unwanted characters and splitting the fullurl string into a list the same length as the number of segments, but this seems incredibly inefficient.

Most PHP frameworks seem to have a method of building an array of the segment variable names regardless of the number but neither Flask, Bottle or Django seem to have a similar option, I seem to need to specify an exact number of segments to capture variables. A couple of PHP cms's seem to collect the first 9 segments immediately as variables and anything any longer gets passed as a full path which is then broken down in the way I mentioned above.

Am I not understanding the way things work in URL routing? Is the string splitting method really inefficient or the best way to do it? Or, is there a way of collecting an unknown number of segments straight into variables in Flask?

I'm pretty new on Python frameworks so a five year olds explanation would help,

many thanks.

解决方案

I'm fairly new to Flask myself, but from what I've worked out so far, I'm pretty sure that the idea is that you have lots of small route/view methods, rather than one massive great switching beast.

For example, if you have urls like this:

http://example.com/unit/57/
http://example.com/unit/57/page/23/
http://example.com/unit/57/page/23/edit

You would route it like this:

@app.route('/unit/<int:unit_number>/')
def display_unit(unit_number):
    ...

@app.route('/unit/<int:unit_number>/page/<int:page_number>/')
def display_page(unit_number, page_number):
    ...

@app.route('/unit/<int:unit_number>/page/<int:page_number>/edit')
def page_editor(unit_number, page_number):
    ...

Doing it this way helps to keep some kind of structure in your application and relies on the framework to route stuff, rather than grabbing the URL and doing all the routing yourself. You could then also make use of blueprints to deal with the different functions.

I'll admit though, I'm struggling to think of a situation where you would need a possibly unlimited number of sections in the URL?

这篇关于Flask和其他Python框架中的多个URL段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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