按照书上的步骤来的
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 % }
404.html中花括號和百分號間不要有空格
你的python檔案中漏了一個bootstrap = Bootstrap(app)
少了bootstrap =
模板引擎無法找到bootstrap模組目錄下的模板
python腳本裡第五行:Bootstrap(app)
應該找個變數保存起來,如:bootstrap = Bootstrap(app)
不然引用計數為0,又給釋放掉了,等於沒初始化。