使用add_url()添加的烧瓶删除路由 [英] Flask delete routes added with add_url()

查看:45
本文介绍了使用add_url()添加的烧瓶删除路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从数据库动态加载URL,并使用 app.add_url()添加它们.由于行为类似于CMS,因此我的用户可以删除或更改某些页面的url,因此我有时需要删除映射或重新加载所有url映射.

at app init i'm dynamically loading URL's from the DB, adding them with app.add_url(). As the behavior is CMS like, my user can delete or change the url for some pages so i need to sometimes delete a mapping or reload all url mappings.

有人知道这样做的方法吗?

Does anyone know a way to do this?

谢谢

推荐答案

烧瓶(依赖 Werkzeug )旨在允许用户轻松添加而不是删除路由.但是,您可以尝试自己删除路由.每个路由都添加到 Flask.add_url_rule()方法.从 Map._rules Map._rules_by_endpoint 中删除路线可能就足够了(请参阅

Flask (which relies on Werkzeug) is designed to allow the user to easily add, not delete, routes. However you can try to delete routes yourself; each route is added to url_map in the Flask.add_url_rule() method. It is probably enough to remove the route from Map._rules and Map._rules_by_endpoint (see the Map.add() method) and call Map.update() with _remap.

但是,这通常不会起作用,例如,当在单独的 dict 中创建委托给视图函数的路由时:

But this will not work in general, for example when creating a route that delegates to a view function in a separate dict:

_routes = {}

@app.route('/<path:url>', marthods=['GET', 'POST'])
def route(url):
    handler = _routes.get(url, None)
    if handler is not None:
        return handler()
    abort(404)

这篇关于使用add_url()添加的烧瓶删除路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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