2014-09-18 95 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)