2012-06-10 19 views
8

我正在使用Flask 0.8。如何在Python Flask上使用URL的別名?

如何有這樣的URL的別名:

@app.route('/') 
def index(): 
    # I want to display as http://localhost/index, BUT, I DON'T WANT TO REDIRECT. 
    # KEEP URL with only '/' 

@app.route('/index') 
def index(): 
    # Real processing to display /index view 

那麼,爲什麼我希望使用別名,因爲加工/指數的幹

有人知道該如何解決?

感謝胡椒粉。

回答

14

這應該工作。但爲什麼你想要兩個URL顯示相同的東西?

@app.route('/') 
@app.route('/index') 
def index(): 
    ... 
+0

感謝所有,解決了。 – hof0w

2

我不知道是否有瓶的方式來分配不止一個URL到視圖功能,但你當然可以把它們連這樣的:

@app.route('/') 
def root(): 
    return index() 

@app.route('/index') 
def index(): 
    # Real processing to display /index view 
5

由於是寫在URL registry doc of Flask

您還可以定義具有相同功能的多個規則。但他們必須 是唯一的。

@app.route('/users/', defaults={'page': 1}) 
@app.route('/users/page/<int:page>') 
def show_users(page): 
    pass 
相關問題