Peterdeka Peterdeka - 1 year ago 116
Python Question

Flask delete routes added with add_url()

at app init i'm dynamically loading URL's from the DB, adding them with

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?


Answer Source

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.

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()