简体   繁体   English

python / Flask:使用动态第一个组件进行路由

[英]python/Flask: Route with dynamic first component

I am writing a Flask site for which I would like to have routes like this: 我正在编写一个Flask网站,我希望为其提供以下路由:

@app.route('/')
@app.route('/<page_id>')
@app.route('/<page_id>/<subpage_id>')
def page(page_id=None, subpage_id=None):
    ...

While it seems like this should work in theory, it looks like this actually breaks static resources located in the root static/ directory. 虽然看起来这在理论上应该可行,但看起来这实际上破坏了位于根static /目录中的静态资源。 I assume the reason for this is that my dynamic route actually matches 'static/style.css' and thus overrides the normal handler for static files. 我认为这是因为我的动态路由实际上与“ static / style.css”匹配,因此覆盖了静态文件的常规处理程序。

Is there any way around this? 有没有办法解决? Is there a 'static' handler I can forward the request to if I detect that page_id=='static'? 如果检测到page_id =='static',是否可以将请求转发给“静态”处理程序?

Edit: Here is a working sample 编辑:这是一个工作示例

@app.route('/<page_id>/<subpage_id>/<subsubpage_id>')
def xxx(page_id=None, subpage_id=None, subsubpage_id=None):
    return 'hello'

If you open http://127.0.0.1:5000/static/css/style.css now you should get a 'hello' instead of the file. 如果您现在打开http://127.0.0.1:5000/static/css/style.css ,则应该得到一个“ hello”而不是文件。

Regarding the root of your problem: 关于问题的根源:

Yes. 是。 I have Page objects in my database, which I load and display based on the page_id/subpage_id/subsubpage_id when the method is called. 我的数据库中有Page对象,在调用该方法时,它们根据page_id / subpage_id / subsubpage_id加载和显示。 Is there a better way to do this? 有一个更好的方法吗? I was thinking of adding separate routes for each of the pages when the app is initialized, but I could not find a good way of making that work in conjunction with url_for. 当我初始化应用程序时,我曾想为每个页面添加单独的路由,但我找不到与url_for结合使用以使其正常工作的好方法。

You can register route handlers directly by using app.add_url_rule . 您可以使用app.add_url_rule直接注册路由处理程序。 It will use the function's name for url_for by default, yes, but you can override that by passing an endpoint argument. 默认情况下,它将使用url_for的函数名称,是的,但是您可以通过传递endpoint参数来覆盖它。

So maybe you'd have something like this: 所以也许您会有这样的事情:

from functools import partial

def actual_handler(page):
    return u'hello'

for page in session.query(Page):
    route = u'/{0}/{1}/{2}'.format(page.id1, page.id2, page.id3)
    app.add_url_rule(
        route,
        page.name,  # this is the name used for url_for
        partial(actual_handler, page=page),
    )

Getting the session may or may not be tricky, and may or may not involve work like manually calling session.remove() ; 获取会话可能不很麻烦,并且可能会或可能不会涉及手动调用session.remove() I haven't tried using SQLAlchemy outside a Flask handler before. 我以前没有尝试过在Flask处理程序之外使用SQLAlchemy。 Assuming you're using SQLA in the first place. 假设您首先使用SQLA。

Also see the documentation on route handling . 另请参阅有关路线处理文档

As for the original question of routes taking priority over static files, I genuinely don't know; 至于路由优先于静态文件的原始问题,我真的不知道。 based on my reading of the Flask and Werkzeug docs, that shouldn't happen. 根据我对Flask和Werkzeug文档的阅读,这不应该发生。 If you still wish to solve this by manually serving static files, perhaps send_from_directory will help. 如果您仍然希望通过手动提供静态文件来解决此问题, send_from_directory可能会有所帮助。 Presumably your web server will serve static files directly in production, anyway, so it might not be worth the metaprogramming gunk above. 大概您的Web服务器无论如何都将直接在生产环境中提供静态文件,因此上面的元编程难题可能不值得。

PS: An afterthought; PS:事后的想法; Pyramid's traversal might be a better fit if your entire site lives in a database. 如果您的整个站点都位于数据库中,则金字塔的遍历可能更合适。 It examines path components one at a time dynamically, rather than having a fixed list of static routes. 它一次动态地检查路径组件,而不是具有固定的静态路由列表。

This is a horrible hack but you could probably just do something akin to: 这是一个可怕的骇客,但您可能可以做一些类似于以下的事情:

@app.route('/<page_id>/<subpage_id>/<subsubpage_id>')
def xxx(page_id=None, subpage_id=None, subsubpage_id=None):
    if page_id == 'static':  # or whatever the path is to your assets
       # make a response where you've filled the request yourself
    return 'hello'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM