【问题标题】:Flask exception handling: page not showing烧瓶异常处理:页面未显示
【发布时间】:2020-04-29 00:37:29
【问题描述】:

我有一个 Flask 网络应用程序。我的应用程序容易出现“HTTP 请求过多错误 429”,因此我想处理该异常。在网上搜索后,我想出了这个:

@app.errorhandler(TooManyRequests)
def handle_too_many_requests(e):
   return render_template('tooManyrequests.html')

我像这样导入了 TooManyRequests:

from werkzeug.exceptions import TooManyRequests

当发生此错误时,我的应用程序仍然显示堆栈树,而不是显示“tooManyRequests.html”,说明发生了错误。如何显示我想要的页面,而不是显示所有代码的默认“不要惊慌”?

编辑:好的,当我将 app.run(debug=True) 更改为 app.run() 时,“不要惊慌”现在已经消失了。但是,虽然它不显示任何代码,但它只是显示一个基本的 html 页面,当异常发生时显示“内部服务器错误”,而不是显示我的自定义 tooManyRequests.html

【问题讨论】:

    标签: python flask


    【解决方案1】:

    您似乎正在调试模式下运行您的应用程序

    你应该以app.run() 运行你的应用程序

    【讨论】:

    • 我把app.run(debug = True),我应该只做app.run()吗?
    • 好吧,我刚刚做了 app.run(),但它显​​示了一些默认的 html“内部服务器错误”页面,而不是显示我想要显示的页面。
    • 把return改成这个render_template(),429
    • 另外,render_template('tooManyrequests.html'), 429 似乎不起作用
    • Flask 开发服务器也不能一次处理多个请求
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-04
    • 1970-01-01
    相关资源
    最近更新 更多