Python Flask 自定义404错误

复制代码
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 "出现了404错误 错误信息是%s" % err


if __name__ == '__main__':
    app.run(debug=True)
相关推荐
进击的明明18 小时前
当AI开始“边想边干”:深入聊聊ReAct,以及如何用Python手搓一个简单Agent
python·面试·agent
全栈弄潮儿19 小时前
Python实战第8期:函数
后端·python·编程语言
卷无止境21 小时前
暗流与破局:AI时代的代码安全审查工具全景指南
后端·python
卷无止境1 天前
一人公司的全栈选型指南:让 AI Agent 写得快,也让生意跑得稳
后端·python
WJ.Polar1 天前
Java封装、继承、多态
java·开发语言
摇滚侠1 天前
Java 中如何表示负数,有符号和无符号的区别,补码的计算规则,char byte short int long 的范围
java·开发语言
滴啦嘟啦哒1 天前
【具身笔记】【π0.5 微调与仿真】五、在MuJoCo中实现可视化仿真
笔记·python·vla·具身
weixin_307779131 天前
C++代码实现MATLAB中的fitrtree函数功能
开发语言·c++·算法·matlab
全栈练习生1 天前
AI安全与红队测试
python·ai
爱读书的小胖1 天前
Codewars:Rot13(C)解题分析
c语言·开发语言