搜索

首页  >  问答  >  正文

python - flaskweb开发这本书中,自定义错误页面无法渲染?

按照书上的步骤来的
1.在bootstrap中编写了base.html,放在templates/bootstrap目录下的
代码:

  1 {% block doc -%}
  2 <!DOCTYPE html>
  3 <html{% block html_attribs %}{% endblock html_attribs %}>
  4 {%- block html %}
  5   <head>
  6     {%- block head %}
  7     <title>{% block title %}{{title|default}}{% endblock title %}</title>
  8 
  9     {%- block metas %}
 10     <meta name="viewport" content="width=device-width, initial-scale=1.0">
 11     {%- endblock metas %}
 12 
 13     {%- block styles %}
 14     <!-- Bootstrap -->
 15     <link href="{{bootstrap_find_resource('css/bootstrap.css', cdn='bootstrap')}}" rel="stylesheet">
 16     {%- endblock styles %}
 17     {%- endblock head %}
 18   </head>
 19   <body{% block body_attribs %}{% endblock body_attribs %}>
 20     {% block body -%}
 21     {% block navbar %}
 22     {%- endblock navbar %}
 23     {% block content -%}
 24     {%- endblock content %}
 25 
 26     {% block scripts %}
 27     <script src="{{bootstrap_find_resource('jquery.js', cdn='jquery')}}"></script>
 28     <script src="{{bootstrap_find_resource('js/bootstrap.js', cdn='bootstrap')}}"></script>
 29     {%- endblock scripts %}
 30     {%- endblock body %}
 31   </body>
 32 {%- endblock html %}
 33 </html>
 34 {% endblock doc -%}

然后在templates下编写了base.html

  1 {% extends "bootstrap/base.html" %}
  2 {% block title %}Flasky{% endblock %}
  3 {% block navbar %}
  4 
  5 
  6 <p class="navbar navbar-inverse" role="navigation">
  7   <p class="container">
  8     <p class="navbar-header">
  9       <button type="button" class="navbar-toggle" 
 10         data-toggle="collapse" data-target=".navbar-collapse">
 11         <span class="sr-only">Toggle navigation</span>
 12         <span class="icon-bar"></span>
 13         <span class="icon-bar"></span>
 14         <span class="icon-bar"></span>
 15       </button>
 16       <a class="navbar-brand" href="/">Flasky</a>
 17     </p>
 18     <p class="navbar-collapse collapse">
 19       <ul class="nav navbar-nav">
 20         <li><a href="/">Home</a></li>
 21       </ul>
 22     </p>
 23   </p>
 24 </p>
 25 
 26 
 27 {% endblock %}
 28 
 29 {% block content %}
 30 
 31 
 32 <p class="container">
 33   {% block page_content %}{% endblock %}
 34 </p>
 35 
 36 
 37 {% endblock %}

还有404.html

  1 { % extends "base.html" % }
  2 { % block title % }Flasky - Page Not Found{ % endblock % }
  3 { % block page_content % }
  4 <p class="page-header">
  5         <h1>Not Found</h1>
  6 </p>
  7 { % endblock % }
~                       

接着是python脚本语言

  1 from flask import Flask,render_template
  2 from flask_bootstrap import Bootstrap
  3 
  4 app = Flask(__name__)
  5 Bootstrap(app)
  6 
  7 @app.route('/user/<name>')
  8 def user(name):
  9     return render_template('user.html', name = name)
 10 @app.errorhandler(404)
 11 def page_not_found(e):
 12     return render_template('404.html'),404
 13 
 14 @app.errorhandler(500)
 15 def internal_server_error(e):
 16     return render_template('500.html'),500
 17 if __name__ == "__main__":
 18     app.run()

可是最后在网页上却显示这样的结果:

我早浏览器中输入的地址是:
127.0.0.1:5000/ab

{ % extends "base.html" % } { % block title % }Flasky - Page Not Found{ % endblock % } { % block page_content % }
Not Found
{ % endblock % }

阿神阿神2795 天前553

全部回复(3)我来回复

  • PHP中文网

    PHP中文网2017-04-18 10:33:30

    404.html中花括号和百分号间不要有空格

    回复
    0
  • PHP中文网

    PHP中文网2017-04-18 10:33:30

    你的python文件中漏了一个bootstrap = Bootstrap(app)
    少了bootstrap =
    模板引擎无法找到bootstrap模块目录下的模板

    回复
    0
  • PHP中文网

    PHP中文网2017-04-18 10:33:30

    python脚本里第五行:Bootstrap(app)
    应该找个变量保存起来,如:bootstrap = Bootstrap(app)
    不然引用计数为0,又给释放掉了,等于没初始化。

    回复
    0
  • 取消回复