1 回答

TA貢獻(xiàn)1859條經(jīng)驗(yàn) 獲得超6個贊
我檢查了add_route的源代碼
它添加route
到兩個對象:self.routes
and?self.router
(?app.routes
and?app.router
) 這會產(chǎn)生問題。
def?add_route(self,?route):? ???"""?Add?a?route?object,?but?do?not?change?the?:data:`Route.app` ????????attribute.""" ????self.routes.append(route) ????self.router.add(route.rule,?route.method,?route,?name=route.name)????if?DEBUG:?route.prepare()
self.router
是具有rules
,?builder
,?static
?dyna_routes
,?_dyna_regexes
如果您在添加新路線之前和之后檢查它們,那么您會看到builder
和中的變化static
。
def addRoute():
? ? print('--- before ---')
? ? print(app.router.rules)
? ? print(app.router.builder)
? ? print(app.router.static)
? ? print(app.router.dyna_routes)
? ??
? ? app.route('/route/hello')(lambda :'Hello World')
? ? print('--- after ---')
? ? print(app.router.rules)
? ? print(app.router.builder)
? ? print(app.router.static)
? ? print(app.router.dyna_routes)
? ? print('Routes after calling /add:\n' + '\n'.join([str(route) for route in app.routes]))
? ? redirect('route/hello')
如果我'/route/hello'從中刪除builder然后停止工作但仍然顯示它們,那么你將不得不從兩者中刪除static-但它們沒有為此的特殊功能:)'/route/hello'app.routes'/route/hello'app.routesapp.router
def removeRoute():
? ? prefix = '/route/hello'
? ? del app.router.builder[prefix]
? ? del app.router.static['GET'][prefix]
? ??
? ? print('Routes after calling /remove:\n' + '\n'.join([str(route) for route in app.routes]))
? ? redirect('/route/hello')
我的完整代碼:
import bottle
from bottle import redirect, abort
def addRoute():
? ? print('--- before /add ---')
? ? print(app.router.rules)
? ? print(app.router.builder)
? ? print(app.router.static)
? ? print(app.router.dyna_routes)
? ? print(app.router.dyna_regexes)
? ? print('Routes before calling /add:\n' + '\n'.join([str(route) for route in app.routes]))
? ??
? ? app.route('/route/hello')(lambda :'Hello World')
? ??
? ? print('--- after /add ---')
? ? print(app.router.rules)
? ? print(app.router.builder)
? ? print(app.router.static)
? ? print(app.router.dyna_routes)
? ? print(app.router.dyna_regexes)
? ? print('Routes after calling /add:\n' + '\n'.join([str(route) for route in app.routes]))
? ??
? ? redirect('route/hello')
def removeRoute():
? ? prefix = '/route/hello'
? ? print('--- before /remove ---')
? ? print(app.router.rules)
? ? print(app.router.builder)
? ? print(app.router.static)
? ? print(app.router.dyna_routes)
? ? print(app.router.dyna_regexes)
? ? print('Routes before calling /remove:\n' + '\n'.join([str(route) for route in app.routes]))
? ? del app.router.builder[prefix]
? ? del app.router.static['GET'][prefix]
? ??
? ? print('--- after /remove ---')
? ? print(app.router.rules)
? ? print(app.router.builder)
? ? print(app.router.static)
? ? print(app.router.dyna_routes)
? ? print(app.router.dyna_regexes)
? ? print('Routes before calling /remove:\n' + '\n'.join([str(route) for route in app.routes]))
? ? redirect('/route/hello')
if __name__ == '__main__':
? ? app = bottle.app()
? ? app.route('/add')(addRoute)
? ? app.route('/remove')(removeRoute)
? ? print('Initial routes:\n' + '\n'.join([str(route) for route in app.routes]))
? ? bottle.run(app, host = 'localhost', port = 8080)
添加回答
舉報