Python 使用参数、瓶子或烧瓶将多页路由到同一模板
我希望使用一个参数将多个页面路由到示例模板,并让其他静态文件也自动路由:Python 使用参数、瓶子或烧瓶将多页路由到同一模板,python,bottle,Python,Bottle,我希望使用一个参数将多个页面路由到示例模板,并让其他静态文件也自动路由: @route('/test1') @route('/hello2') @route('/page3') @view('tpl/page.html') def page(): context = {'request': request, 'pagename': ??} return (context) @route('/<filename>') def files(filename):
@route('/test1')
@route('/hello2')
@route('/page3')
@view('tpl/page.html')
def page():
context = {'request': request, 'pagename': ??}
return (context)
@route('/<filename>')
def files(filename):
return static_file(filename, root='./static/')
我希望:
This is the page that was requested: hello2
如何将多个页面定向到同一模板,并访问页面名
我尝试了
str(request).split('example.com/')[1]。replace('>','')
但这是一个肮脏的黑客行为,我想有一个更干净的方法来获取test1
,hello2
,等等。从@route
你可以这样做:
@route('/test1', route_name='test1')
@route('/hello2', route_name='hello2')
@route('/page3', route_name='page3')
@view('tpl/page.html')
def page(route_name):
context = {'request': request, 'pagename': route_name}
return (context)
Flask会自动将不匹配的参数传递给路由。这适用于Flask,我认为它也适用于Flask,但事实并非如此:
TypeError('page()正好接受1个参数(0给定)'),
对于Baggle来说,这要复杂得多,因为他们的路由装饰器实际上修改了修饰过的函数。这使得无法使用上述方法。我认为您唯一的选择是编写一个路由插件,检查路径并传递正确的路由名称。
@route('/test1', route_name='test1')
@route('/hello2', route_name='hello2')
@route('/page3', route_name='page3')
@view('tpl/page.html')
def page(route_name):
context = {'request': request, 'pagename': route_name}
return (context)