Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask delete routes added with add_url()

Tags:

python

flask

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?

Thanks

like image 629
Peterdeka Avatar asked Oct 21 '22 07:10

Peterdeka


1 Answers

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()
    abort(404)
like image 110
tbicr Avatar answered Oct 27 '22 11:10

tbicr