404 500 flask

筆記種類
flask

並不是在blueprint裡面創404的route,而是創在__init__ def create_app():裡

這時你想可能直接在routes裡加就好了

pages = Blueprint(
    "pages" ,
    __name__ ,
    template_folder="templates",
    static_folder="static"
)



@pages.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404

@pages.errorhandler(500)
def Server_Error(e):
    return render_template('500.html'), 500


但你直接加在pages裡會沒效,要加在__init__.py裡

def create_app():

  @app.errorhandler(404)
    def page_not_found(e):
        return render_template('404.html'), 404

    @app.errorhandler(500)
    def Server_Error(e):
        return render_template('500.html'), 500

    return app

記得要創404.html 的頁面出來

{% from "macros/fields.html" import render_text_field %}
{% extends "basic.html" %}
{% block css_style %}<link rel="stylesheet" href="../static/404.css">{% endblock %}

{% block main_content %}
<div class="main">
    <div class="main_describtion">
        <h1 class="main_title">404</h1>
        <p>看起來幽靈把頁面吃掉了我正試著把它找回來</p>
    </div>
    <img src="../static/pic/404.gif" alt="" class="main_pic">
</div>
{% endblock %}