2013-03-14 70 views
-2

上面的代碼在我的燒瓶應用程序中拋出錯誤。燒瓶 - 重定向url_for錯誤

@@app.route('/') 
..... 

return redirect(url_for('nextPage'),id=DBTable.id) 


@app.route('/<path:id>') 
@login_required 
def nextPage(id): 
return render_template('page2.html')    

Error - 
--------------------------------------------------------------------------- 
File "C:\Python27\lib\site-packages\werkzeug\routing.py", line 1607, in build 
raise BuildError(endpoint, values, method) 
BuildError: ('nextPage', {}, None) 
<SocketIOServer fileno=116 address=0.0.0.0:5000>: Failed to handle request: 
request = POST /landingPage HTTP/1.1 from ('127.0.0.1', 50287) 
application = <flask.app.Flask object at 0x0000000002643B70> 

請幫我與上面​​的問題

+0

嘗試添加「/」在路線的終點爲@ app.route('/ /') – rajpy 2013-08-28 07:48:54

回答

0

而且所有明顯的語法錯誤,問題應該通過放置您傳遞到url_for塊內的路由參數來解決。

@app.route('/') 
def index(): 
    # ... 
    return redirect(url_for('nextPage', id=DBTable.id)) 

@app.route('/<id>') 
def nextPage(id): 
    # ... 
    return render_template('page2.html') 
+0

感謝Teisman的迴應,但改變後仍然看到同樣的錯誤。 – user2104391 2013-03-14 09:08:31

+0

我很抱歉聽到這個消息。你確定它是一樣的錯誤嗎?我問這是因爲它在我的機器上運行。在調試時轉移到更簡單的代碼從未受傷。由於我們暫時不使用'id',請嘗試填充虛擬值,如'test',以便它變成'return redirect(url_for('nextPage',id =「test」))',並且檢查是否再次拋出相同的錯誤。 – Teisman 2013-03-14 09:32:51

3

嘗試,如果你有任何錯誤使用此代碼

from flask import * 
app     = Flask(__name__) 
#configuration 
@app.route('/') 
def index(): 
    return redirect(url_for('random', id="blah blah")) 

@app.route('/<id>') 
def random(id): 
    return id 
if __name__ == '__main__': 
    app.run(debug=True) 

,並告訴我