Python烧瓶重定向错误

时间:2014-09-18 18:07:36

标签: python exception web flask

我想在发生异常时重定向到包含错误代码的注册页面。我怎么能在烧瓶中做到这一点?如何使用错误代码重定向到同一页面?

@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)

1 个答案:

答案 0 :(得分: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)