2014-09-18 120 views
1

我想在出現異常時重定向到帶有錯誤代碼的註冊頁面。我如何在燒瓶中做到這一點?我如何重定向到錯誤代碼的同一頁面?Python燒瓶重定向錯誤

@app.route('/signup', methods=['GET','POST']) 
def signup(): 
    error = None 
    if request.method == 'POST': 
    try: 
     ... my code ... 
    except Exception, e: 
     error = "hey this is error" 
     ... i want to redirect to signup with error ... 
     ... i get only some stacktrace page due to debug ... 
    return redirect(url_for('login')) 
    return render_template('signup.html', error=error) 

回答

2

您需要放置try/except依賴的return語句來處理該語句。問題是,無論try /中發生了什麼,除非它會進入if語句,否則它總是會進入登錄頁面。你需要相應地分解你的回報。

@app.route('/signup', methods=['GET','POST']) 
def signup(): 
    error = None 
    if request.method == 'POST': 
     try: 
      ... my code ... 
      return redirect(url_for('login')) 
     except Exception, e: 
      error = "hey this is error" 
      ... i want to redirect to signup with error ... 
      return render_template('signup.html', error=error) 
    return render_template('signup.html', error=error)