Flask:蓝图不显示自定义错误页面

AbtPst

这是我创建简单蓝图的方式

from flask import Blueprint, render_template

my_bp_page = Blueprint('my_bp_page', __name__, template_folder='templates')

@my_bp_page.errorhandler(404)
def page_not_found(e):
    return render_template('404.html', pageName='my_bp_page.home')


@my_bp_page.route('/home', methods=['GET'], strict_slashes=False)
def home():
    return render_template('home.html')

这是我的 404.html

{% block title %}Page Not Found{% endblock %}
{% block body %}
  <h1>Page Not Found</h1>
  <p>What you were looking for is just not there.
  <p>
    {% if pageName is defined %}
        <a href="{{ url_for(pageName) }}">Go Back</a>
    {% else %}
        <a href="{{ url_for('home') }}">Go Back</a>
    {% endif %}
{% endblock %}

在测试时,我尝试了URL,localhost:5000/home/junk然后看到

Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

这不是我应该看到的。我应该看看我的风俗404.html

我究竟做错了什么 ?

皮埃尔五世

问题是,就Flask而言,该/home/junk路径不属于您的my_bp_page蓝图。这只是一条未注册任何路径的路径。

蓝图文档的“错误处理程序”部分(第3段)中提到了此警告

推荐的解决方案是使用应用程序级错误处理程序,并检查request.path以自定义处理错误的方式。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章