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)
相关推荐
Flittly8 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(3)TodoWrite (待办写入)
python·agent
千寻girling13 小时前
一份不可多得的 《 Django 》 零基础入门教程
后端·python·面试
stark张宇13 小时前
构建第一个AI聊天机器人:Flask+DeepSeek+Postgres实战
人工智能·postgresql·flask
databook16 小时前
探索视觉的边界:用 Manim 重现有趣的知觉错觉
python·动效
明月_清风17 小时前
Python 性能微观世界:列表推导式 vs for 循环
后端·python
明月_清风17 小时前
Python 性能翻身仗:从 O(n) 到 O(1) 的工程实践
后端·python
helloweilei1 天前
python 抽象基类
python
用户8356290780511 天前
Python 实现 PPT 转 HTML
后端·python
zone77392 天前
004:RAG 入门-LangChain读取PDF
后端·python·面试
zone77392 天前
005:RAG 入门-LangChain读取表格数据
后端·python·agent