动态地将URL规则添加到Flask应用程序 [英] Dynamically add URL rules to Flask app
问题描述
我正在编写一个应用程序,用户可以在其中存储可以指定REST接口的信息。 IE,在 /< username> / rest / products
中存储产品列表。由于URL之前显然不知道,所以我试图想到在Flask中实现动态URL创建的最佳方法。我想到的第一种方法是编写一个全面的规则,并从那里路由URL。但是,当Flask已经内置了它们的时候,我基本上重复了URL路由功能。所以,我想知道是否使用 .add_url_rule()
( docs here ,向下滚动一下)直接附加到应用程序。有没有一个特定的原因,这不应该做的?
每次执行 add_url_rule
内部路由重新映射URL地图。这既不是线程安全也不是快速的。我现在不明白你为什么需要用户特定的URL规则是诚实的。这听起来像你真的想要用户特定的应用程序装入?
也许这是有帮助的: http://flask.pocoo.org/docs/patterns/appdispatch/
I am writing an app in which users will be able to store information that they can specify a REST interface for. IE, store a list of products at /<username>/rest/products
. Since the URLs are obviously not known before hand, I was trying to think of the best way to implement dynamic URL creation in Flask. The first way I thought of would be to write a catch-all rule, and route the URL from there. But then I am basically duplicating URL routing capabilities when Flask already has them built-in. So, I was wondering if it would be a bad idea to use .add_url_rule()
(docs here, scroll down a bit) to attach them directly to the app. Is there a specific reason this shouldn't be done?
Every time you execute add_url_rule()
the internal routing remaps the URL map. This is neither threadsafe nor fast. I right now don't understand why you need user specific URL rules to be honest. It kinda sounds like you actually want user specific applications mounted?
Maybe this is helpful: http://flask.pocoo.org/docs/patterns/appdispatch/
这篇关于动态地将URL规则添加到Flask应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!