Python烧瓶重定向错误

Python烧瓶重定向错误,python,exception,web,flask,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"

当发生异常时,我想重定向到带有错误代码的注册页面。我怎么能在烧瓶里做这个?如何重定向到带有错误代码的同一页

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

您需要放置try/except依赖返回语句来处理这个问题。问题是,无论try/except中发生什么,如果它输入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)