【问题标题】:Python flask redirect with errorPython烧瓶重定向错误
【发布时间】:2014-11-13 04:45:45
【问题描述】:

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

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

【问题讨论】:

    标签: python exception web flask


    【解决方案1】:

    您需要放置 try/except 依赖的 return 语句来处理它。问题是,无论 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)
    

    【讨论】:

      猜你喜欢
      • 2017-05-11
      • 1970-01-01
      • 2014-04-14
      • 2015-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-17
      • 1970-01-01
      相关资源
      最近更新 更多