python - Match an arbitrary path, or the empty string, without adding multiple Flask route decorators -


i want capture urls beginning prefix /stuff, following examples match: /users, /users/, , /users/604511/edit. write multiple rules match everything. there way write 1 rule match want?

@blueprint.route('/users') @blueprint.route('/users/') @blueprint.route('/users/<path:path>') def users(path=none):     return str(path) 

it's reasonable assign multiple rules same endpoint. that's straightforward solution.


if want 1 rule, can write custom converter capture either empty string or arbitrary data beginning slash.

from flask import flask werkzeug.routing import baseconverter  class wildcardconverter(baseconverter):     regex = r'(|/.*?)'     weight = 200  app = flask(__name__) app.url_map.converters['wildcard'] = wildcardconverter  @app.route('/users<wildcard:path>') def users(path):     return path  c = app.test_client() print(c.get('/users').data)  # b'' print(c.get('/users-no-prefix').data)  # (404 not found) print(c.get('/users/').data)  # b'/' print(c.get('/users/400617/edit').data)  # b'/400617/edit' 

if want match anything prefixed /users, example /users-no-slash/test, change rule more permissive: regex = r'.*?'.


Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -