zoukankan      html  css  js  c++  java
  • 自定制404错误页码

    由于系统的在看flask,发现了这个神奇的东西

    使用场景:

    如果你在浏览器的地址栏中输入了不可用的路由,那么会显示一个状态码为 404 的错误页 面。
    
    404,客户端请求未知页面或路由时显示;500,有未处理的异常时显示。

    详细代码

    from flask import Flask, render_template
    
    app = Flask(__name__)
    
    
    @app.route('/')
    def index():
        name = "<h1>Hello</h1>"
        return render_template('index.html', name=name)
    
    
    @app.errorhandler(404)
    def url_error(e):
        return render_template('index.html', name=404), 404
    
    
    @app.errorhandler(500)
    def url_error2(e):
        return render_template('500.html'), 500
    
    
    if __name__ == '__main__':
        app.run(debug=True)
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <h1>hello, {{ name }}</h1>
    </body>
    </html>
    自定义错误子页面,名称可以自定制,也可以500.html
  • 相关阅读:
    2016四川省赛 Floyd-Warshall
    CF374 Maxim and Array
    CF374 Journey
    hdu5730 Shell Necklace
    hihocoder1388 Periodic Signal
    hihocoder1391 Country
    hdu 5903 Square Distance
    hdu5904 LCIS
    Python学习-2.安装IDE
    Python学习-1.安装Python
  • 原文地址:https://www.cnblogs.com/renfanzi/p/6080962.html
Copyright © 2011-2022 走看看