Python Flask 自定义错误页面

 新建templates/404.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<img src="../static/a123.png" alt="" width="873" height="626">

</body>
</html>

位置不要放错了 还是在templates同级别添加static目录 放入错误图片a123.png

当提交错误的代码的时候会返回错误页面

from flask import Flask, abort, make_response, request, render_template

app = Flask(__name__)


# 重定向到百度
@app.route('/index', methods=["GET", "POST"])
def index():
    if request.method == "GET":
        return render_template("index.html")
    if request.method == "POST":
        name = request.form.get('name')
        password = request.form.get('password')
        if name == 'zhangsan' and password == '123':
            return '登录成功'
        else:
            abort(404)


@app.errorhandler(404)
def handle_404_error(err):
    return render_template("404.html")


if __name__ == '__main__':
    app.run(debug=True)


非常的时尚。

相关推荐

  1. 如何定义SpringBoot的白标错误页面

    2024-03-27 19:46:03       9 阅读
  2. QB web 定义页面

    2024-03-27 19:46:03       26 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-27 19:46:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-27 19:46:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-27 19:46:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-27 19:46:03       20 阅读

热门阅读

  1. Mac OS 开启原生NTFS读写功能

    2024-03-27 19:46:03       16 阅读
  2. vue3 + ts + vite 支持低版本的浏览器

    2024-03-27 19:46:03       18 阅读
  3. Python数据分析三

    2024-03-27 19:46:03       14 阅读
  4. 软件开发工具(自考小资料)

    2024-03-27 19:46:03       18 阅读
  5. AI赋能写作:ChatGPT让论文大放异彩

    2024-03-27 19:46:03       18 阅读
  6. 5.85 BCC工具之tcprtt.py解读

    2024-03-27 19:46:03       18 阅读
  7. C++入门

    C++入门

    2024-03-27 19:46:03      18 阅读
  8. Python石头、剪刀、布游戏

    2024-03-27 19:46:03       18 阅读
  9. STEP 格式三维模型读取

    2024-03-27 19:46:03       21 阅读
  10. 算法刷题day36

    2024-03-27 19:46:03       15 阅读