このチュートリアルに従ってください:
http://bottlepy.org/docs/dev/tutorial.html#request-routing
例を示します。
@route('/')
@route('/hello/<name>')
def greet(name='Stranger'):
return template('Hello {{name}}, how are you?', name=name)
そして次のように述べています。
この例は 2 つのことを示しています。1 つのコールバックに複数のルートをバインドできることと、ワイルドカードを URL に追加してキーワード引数を介してアクセスできることです。
次のコードでこれをテストしようとしていますが、インデックスにアクセスすると 500 エラーが発生します/
。
import bottle
import pymongo
custom = bottle
@custom.route('/')
@custom.route('/hello/<name>')
@custom.view('page2.tpl')
def index(name):
# code
bottle.run(host='localhost', port=8082)
エラーは、サイトのインデックスにアクセスするときにのみ発生するよう/
です。
次の例ではインデックスは機能しませんが、他の 2 つのルートでは機能します。
import bottle
import pymongo
custom = bottle
@custom.route('/') # this doesn't work
@custom.route('/milo/<name>') # this works
@custom.route('/hello/<name>') # this works
@custom.view('page2.tpl')
def index(name):
# code
bottle.run(host='localhost', port=8082)
解決
import bottle
import pymongo
custom = bottle
@custom.route('/')
@custom.route('/milo/<name>')
@custom.route('/hello/<name>')
@custom.view('page2.tpl')
def index(name="nowhere"):
# code
bottle.run(host='localhost', port=8082)
nowhere
最初の 2 つのルートのいずれかが使用されない限り出力されます。その場合、その値は何であれ上書きされます<name>
。